1

我有一个下拉列表

  <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 是空的。但是,从代码隐藏中填充它之后是有价值的。它也得到了相同的下拉菜单。

 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>
4

1 回答 1

0

使用视图状态。启用页面和控制 ViewState 。通过确认

IIS -> Your WebSite -> Pages & Controls -> Enable Viewstate & Enable Sessionstate 应设置为 true。

或者

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

于 2012-12-04T15:59:16.590 回答