Why does the Combo.SelectedValue get lost after some time
So I have this asp:DropDownList on a page. It renders like this (in IE7):
<select name="ctl00$cphFilter$cbLista" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphFilter$cbLista\',\'\')', 0)" id="ctl00_cphFilter_cbLista" class="agsSelect">
<option selected="selected" value="4350">A</option>
<option value="4352">B</option>
<option value="4349">C</option>
<option value="4348">D</option>
And then I have a grid and a button on the same page. When the user clicks the button the selected item of the dropdown is read (well a 开发者_JAVA技巧datasource object reads it) and the grid does a databind after a trip to a DB where it gets some data based on that selected value.
This works fine most of the time. However sometimes, the selection in the dropdownlist seems to get lost even though the rendered page says the A is the selected item.
The datasource object is defined like this:
<asp:ObjectDataSource ID="dsVM" runat="server" EnablePaging="False" SelectMethod="Select" SortParameterName="sort" TypeName="X.Business.Entities.LPVM.BE">
<SelectParameters>
<asp:ControlParameter Name="listaId" Type="Int32" ControlID="cphFilter$cbLista" PropertyName="SelectedValue" />
</SelectParameters>
</asp:ObjectDataSource>
Any ideas why the grid would reload its data with a select parameter that is 0 instead of the selected value of the dropdownlist?
EDIT Suppose the dropdownlist is bound, the user selected B and the grid is bound as well and shows the right data. Now, I wait 2 minutes and I click the Refresh button. Surprisingly, at this particular moment the dropdownlist.SelectedValue (which I already know it was 4352 before I clicked because that's how it looks in the rendered page) is actually an empty string. Where did the value go?
At the time of gridview data binding, your dropdown does not bound, so dropdown does not contain value at that time and 0 value passed to objectdatasource. After your gridview data binding, your dropdown data binding is called and you will see the item in the dropdown, you need to lookover databinding hierarchy, like
DropDownList1.DataBind();
GridView1.DataBind();
You mention that the grid does a databind on postback. If you are using a data source for your gridview you shouldn't need to rebind it. On postback asp.net will do that for you.
If you are rebinding manually on postback have you checked that you are not accidentally rebinding the DropDownList which could reset that value?
Possible cause what can be is that your SessionState is being refreshed after a certain time, like 60 seconds. Your selection made will be vanished, because the value isn't hold in the cache anymore.
精彩评论