我想以編程方式設置SqlDataSource的參數,如步驟5中http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb所述。另外一個GridView綁定到sqlDataSource。我的標記是:ASP.NET爲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>
在代碼隱藏,我有:從calcResult()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mySqlDataSource.SelectParameters("subscription_con").DefaultValue = calcResult()
End Sub
的返回值是每次回發不同。回發發生在用戶單擊具有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不會更新回發?
試過了,它沒有工作。 – harrije 2010-06-04 14:46:37
我收回它,它確實有效。我昨天最初嘗試過時,我一定做了其他的事情。 謝謝! – harrije 2010-06-04 14:55:12
這解決了我的幾個問題。非常感謝你 – JohnZ 2014-12-22 17:13:53