ASP.NET setting option for SqlDataSource programmatically not working for postback

I want to programmatically define a parameter for SqlDataSource, as described in step 5, at http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb . GridView is also bound to sqlDataSource. My markup:

<asp:SqlDataSource ID="mySqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringHTL %>" SelectCommand="SELECT [subscription_con] FROM [HTL_CONSUME_CON] WHERE ([subscription_con] = @subscription_con)"> <SelectParameters> <asp:Parameter Name="subscription_con" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:GridView ID="myGridView" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="mySqlDataSource"> </asp:GridView> 

In the code of the code, I have:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load mySqlDataSource.SelectParameters("subscription_con").DefaultValue = calcResult() End Sub 

The return value from calcResult () is different for each postback. Submission occurs when the user clicks a button in a form that has UseSubmitBehavior = True.

I use the debugger to execute the code behind, and I see that it runs for every page load, and I see the expected values ​​returned from clacResult ().

However, the associated DataGrid is never updated in postbacks, it only updates when the first page loads.

If I change the SqlDataSource parameter to have the control as a source, then it works with reverse processing. In other words, I changed the markup to use:

 <asp:ControlParameter ControlID="myTextBox" Name="subscription_con" PropertyName="Text" Type="Int32" /> 

and I changed the code behind:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load myTextBox.Text = calcResult() End Sub 

By using the TextBox control as the source of the SqlDataSource parameter, the GridView update works to load the original page and all postbacks. However, I really do not need a TextBox and prefer not to use it.

What am I missing about how to programmatically set a parameter for SqlDataSource? Why is the associated GridView not updated during postback when parameterizing the SqlDataSource parameter programmatically when there is no control source?

+4
source share
2 answers

You may need an explicit databaind for postbacks:

 if (Page.IsPostBack) { grid.DataBind(); } 

Must do it.

+4
source

FROM#

 SqlDataSource1.SelectParameters["Where_Clause"] = new Parameter() { Name = "Where_Clause", DefaultValue = "WHERE m.Id = 1" }; 

Greedy networks.

+3
source

Source: https://habr.com/ru/post/1311871/


All Articles