问题
I have a site that uses a couple DropDownLists that are databound. I was also doing the traditional if (!IsPostBack) {list.Databind();} and relied on viewstate to keep the lists filled during a post back. I recently converted the site to ASP.NET 3.5 and noticed that the lists are empty during postback (as if ViewState is disabled). I didn't explicitly disable anything, and am wondering if anyone has seen a similar change or behavior in their viewstate dependent controls.
Thanks!
James
回答1:
I have definitely seen similar problems (though mine were with visibility). Try to ensure that the ViewState is explicitly enabled on the whole control hierarchy down to the dropdown.
e.g.
‹asp:Page EnableViewState="True" ...›
...
‹asp:Panel EnableViewState="True"...›
...
‹asp:DropDownList EnableViewState="True" ...›
...
...
...
R.
回答2:
This snippet pasted into the troublesome page is a quick way to see where viewstate is enabled/disabled.
<%
Control c = <YourMisbehavingControlNameHere>;
while ( c != null )
{
Response.Write( c.GetType().Name + " = " + c.EnableViewState.ToString() + "<br/>" );
c = c.Parent;
}
%>
来源:https://stackoverflow.com/questions/445222/switched-to-asp-net-3-5-dropdownlist-doesnt-remember-dynamically-added-items