我想以编程方式设置 SqlDataSource 的参数,如 http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb 。 GridView 也绑定到 sqlDataSource。我的标记是:
<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>
在代码隐藏中,我有:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mySqlDataSource.SelectParameters("subscription_con").DefaultValue = calcResult()
End Sub
calcResult() 的返回值对于每个回发都是不同的。当用户单击表单上 UseSubmitBehavior=True 的按钮时,会发生回发。
我使用调试器单步执行后面的代码,我看到它为每个页面加载执行,并且我看到从 clacResult() 返回的预期值。
但是,绑定的 DataGrid 永远不会在回发时更新,它仅在第一页加载时更新。
如果我更改 SqlDataSource 参数以将控件作为源,那么它可以在回发上工作。换句话说,我将标记更改为使用:
<asp:ControlParameter ControlID="myTextBox" Name="subscription_con" PropertyName="Text" Type="Int32" />
并将后面的代码更改为:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
myTextBox.Text = calcResult()
End Sub
使用 TextBox 控件作为 SqlDataSource 参数的源,GridView 的更新适用于原始页面加载和所有回发。但是,我确实不需要 TextBox,并且不想使用它。
关于如何以编程方式为 SqlDataSource 设置参数,我缺少什么?在没有控制源的情况下,以编程方式设置 SqlDataSource 参数时,为什么绑定的 GridView 不会在回发时更新?
I want to set a parameter for a SqlDataSource programmatically as described in Step 5 at http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb . Also a GridView is bound to the sqlDataSource. My markup is:
<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 codebehind, 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. The postback occurs when the user clicks a Button on the form that has UseSubmitBehavior=True.
I've use the debugger to step through the code behind and I see it executed for each page load and I see the expected values returned from clacResult().
However the bound DataGrid is never updated on the postbacks, it only updates on the first page load.
If I change the SqlDataSource parameter to have a control as the source, then it works on postbacks. In otherwords, I changed the markup to use:
<asp:ControlParameter ControlID="myTextBox" Name="subscription_con" PropertyName="Text" Type="Int32" />
and I changed the code behind to be:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
myTextBox.Text = calcResult()
End Sub
With the TextBox control as the source of the SqlDataSource parameter, update of the GridView works for the original page load and all postbacks. However, I really have no need for the TextBox and would prefer not to use it.
What am I missing about how to set a parameter programmatically for an SqlDataSource? Why doesn't the bound GridView get updated on postbacks when setting the SqlDataSource parameter programmatically when there is no control source?
发布评论
评论(2)
您可能需要一个关于回发的显式数据库:
应该这样做。
You may need an explict databaind on postbacks:
Should do it.
C#
贪婪网络。
C#
Greedy Networks.