C# 回发时的Dropdownlist项目检索问题
我有一个下拉列表C# 回发时的Dropdownlist项目检索问题,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我有一个下拉列表 <asp:DropDownList ID="DropDownListMySubjectz" runat="server"> </asp:DropDownList> 一切正常。直到回发发生。我尝试从下拉列表中获取项目。但是DropDownlist是空的。然而,在从codebehind填充它之后,仍然有价值。它也得到了同样的下拉列表 DropDownList
<asp:DropDownList ID="DropDownListMySubjectz" runat="server">
</asp:DropDownList>
一切正常。直到回发发生。我尝试从下拉列表中获取项目。但是DropDownlist是空的。然而,在从codebehind填充它之后,仍然有价值。它也得到了同样的下拉列表
DropDownList DDL = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
DataSource that populate the gridview
<asp:ObjectDataSource ID="ObjectDataSourceProfileSubjects" runat="server" SelectMethod="GetUserSubjects"
TypeName="ProfileDataAccess">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenFieldUserId" Name="suserId" PropertyName="Value"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
使用ViewState。启用页面和控件ViewState。 确认 IIS->您的网站->页面和控件->启用视图状态并启用 Sessionstate应设置为true 或
在OnInit函数中将数据加载到DropDownList,然后应该正确加载它我猜您的gridview在每次回发时都会刷新自己,并且没有调用您的databind方法。您在代码中的什么时候尝试填充下拉列表?我单击它时有一个链接按钮,它的命令事件被调用,在其中执行此代码。无GridView未刷新。我有很多断点,只有点击才能点击。你的下拉列表是否在更新面板中?您引用的回发是否为完整回发?何时/何地填充GridView?是的,它位于updatepanel内。不,这是部分回发。ViewState在页面或DropDownList上的任何位置都没有禁用。但是数据是加载的,我从项目中进行选择,但是当调用LinkButton命令事件时,它不显示任何项目。@user1698985;是的,这个问题主要发生在dropdownlist上,这肯定是有原因的。我想知道为什么会发生这种情况,以便将来可以跟踪它。@user1698985:OnInit发生在Asp.Net将值从窗体加载到其对象之前。页面加载发生在加载事件期间,该事件发生在查询表单之后。请查看此页面以了解Asp.Net中的页面事件:
DropDownList DDL = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
DataSource that populate the gridview
<asp:ObjectDataSource ID="ObjectDataSourceProfileSubjects" runat="server" SelectMethod="GetUserSubjects"
TypeName="ProfileDataAccess">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenFieldUserId" Name="suserId" PropertyName="Value"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>