What webhierachicaldatagrid or datasource event is considered to be the appropriate place to set select parameters for a database call?
I can set the parameters in the WebHierarchicalDataGrid DataBinding event, but only through the use of each parameters default value property!
Private Sub whdgPackages_DataBinding(sender As Object, e As EventArgs) Handles whdgPackages.DataBinding dsMatrix.SelectParameters("Type").DefaultValue = 1 dsMatrix.SelectParameters("Tier").DefaultValue = CInt(ViewState("TIER")) dsMatrix.SelectParameters("Key").DefaultValue = CInt(ViewState("KEY"))End Sub
Following is the underlying datasource Selecting event for a WebHierarchicalDataGrid. This seems to be getting called multiple times on a postback!!! Not sure if this is the proper place to do this.
Protected Sub dsMatrix_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles dsMatrix.Selecting e.Command.Parameters(0).Value = 1 e.Command.Parameters(1).Value = CInt(ViewState("TIER")) e.Command.Parameters(2).Value = CInt(ViewState("KEY"))End Sub
Hello,
Thank you for using our forum!
My suggestion is to set the parameters into the event handler for your datasource, OnSelecting event.
Something like:
<asp:SqlDataSource ... OnSelecting="ds_Selecting">
..protected void ds_Selecting(object sender, SqlDataSourceCommandEventArgs e){ e.Command.Parameters[0].Value = "Value";}
Let me know if I may be of further assistance.
Thank You Zdravko!
Your advice suggests the second approach I presented in my original post as I am coding the parameter assignment in the code-behind of my webforms application. It is curious though why this event gets executed twice everytime a page postback occurs. Do you have any comment as to that issue?
To be honest I am not entirely sure, it seems that setting parameters in the select event have some drawbacks. The discussion below have s couple of good suggestions that might be helpful, for example setting EnableCaching to true or using of e.ExecutingSelectCount to skip the second execution.
Reference:
http://forums.asp.net/t/1161164.aspx?ObjectDataSource+Select+method+getting+called+twice