下拉列表回发时的项目检索问题

时间:2012-12-04 15:56:15

标签: c# asp.net drop-down-menu

我有一个下拉列表

  <asp:DropDownList ID="DropDownListMySubjectz" runat="server">
                                              </asp:DropDownList>

我尝试在C#中的CodeBehind中填充它。

                                        HtmlGenericControl divMySubjects = (HtmlGenericControl)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("divMySubjects");
                    divMySubjects.Style["display"] = "block";
                    DropDownList mySubjects = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
                    UpdatePanel mySubjectsPanel = (UpdatePanel)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("UpdatePanelRequestAction");
                    mySubjects.DataSource = ProfileDataAccess.GetUserSubjects(Membership.GetUser().ProviderUserKey.ToString()).DefaultView;
                    mySubjects.DataTextField = "Subjects";
                    mySubjects.DataValueField = "SubjectId";
                    mySubjects.DataBind();
                    mySubjectsPanel.Update();

每件事都很好。直到回发发生。我尝试从DRopDownList获取项目。但是DropDownlist是空的。但是,从代码隐藏后填充它有价值。并且它也会得到相同的Dropdown。

 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>

1 个答案:

答案 0 :(得分:0)

使用ViewState。启用页面&amp;控制ViewState。

确认一下
  

IIS - &gt;您的网站 - &gt;页数和控制 - &gt;启用Viewstate&amp;启用   Sessionstate应设置为true。

将数据加载到OnInit函数中的DropDownList,然后应该正确加载