问题
I have a form with:
- 1 text box ("MyTextBox")
- 3 check boxes ("MyCheckBox1", "MyCheckBox2", "MyCheckBox3")
- 1 submit button ("MySubmitButton")
- 1 ObjectDataSource ("MyObjectDataSource")
- 1 GridView ("MyGridView")
My ObjectDataSource looks like this:
<asp:ObjectDataSource ID="MyObjectDataSource" runat="server"
SelectMethod="MySelectMethod"
TypeName="MyTypeName">
<SelectParameters>
<asp:ControlParameter Name="MyRegularString"
Type="String"
ControlID="MyTextBox"
PropertyName="Text" />
<asp:Parameter Name="MySpecialString"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
The second parameter ("MySpecialString") is filled like this:
Protected Sub MyObjectDataSource_Selecting(...) Handles MyObjectDataSource.Selecting
Dim MySpecialString As String = "Key1=" & MyCheckBox1.Checked.ToString & ";" & _
"Key2=" & MyCheckBox2.Checked.ToString & ";" & _
"Key3=" & MyCheckBox3.Checked.ToString & ";"
e.InputParameters.Item("MySpecialString") = MySpecialString
End Sub
Whenever I change the value of MyTextBox and press MySubmitButton, the select method of MyObjectDataSource is invoked and everything works OK.
But if I change the value of any of my check boxes (without changing the value of MyTextBox) and press MySubmitButton, the select method is not invoked, thus the "MyObjectDataSource_Selecting" event doesn't take place and I get the same results as before.
One way I found to work around this was to explicitly invoke MyGridView.DataBind method whenever MySubmitButton was clicked. Is this the best way to handle these type of parameters? What I am afraid is that this may cause the select method to be called twice making the page slower.
Thanks in advance for any help.
CD
回答1:
This is what I ended up doing.
- Replaced all the asp:ControlParameters with asp:Parameters.
- Explicitly invoked MyGridView.DataBind method whenever MySubmitButton was clicked.
I checked and the select method is not invoked twice when doing this.
来源:https://stackoverflow.com/questions/10593015/objectdatasource-select-not-always-firing-on-page-postback