ObjectDataSource.Select() not always firing on page postback

一世执手 提交于 2019-12-24 08:24:50

问题


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.

  1. Replaced all the asp:ControlParameters with asp:Parameters.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!