0

我有 DropDownList ,我必须在与另一个控件的单击事件关联的事件中填充它。数据已填充并存在,但是当我选择一个值并回发时,这些值为空。这意味着视图状态不起作用。解决方案说在 Init() 中填充 DropDown 但我不能,因为要求不允许这样做。我必须在点击事件上填充它.. Viewstates 已启用.. 以下代码填充了 DropDown..

if (e.CommandName == "Add Friend")         
{
   HtmlGenericControl divMySub = (HtmlGenericControl)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("divMySubjects");
   divMySub.Style["display"] = "block";

   DropDownList mySub = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
   UpdatePanel mySubPanel = (UpdatePanel)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("UpdatePanelRequestAction");
   DataView SubjectTableView = ProfileDataAccess.GetUserUnusedSubjectsForRequest(UserId ,RequesterId).DefaultView;
   if (SubjectTableView.Count > 0)
   {
       mySub.DataSource = SubjectTableView;
       mySub.DataTextField = "Name";
       mySub.DataValueField = "Id";
       mySub.DataBind();

   } 
   else
   {
       divMySub.InnerText = "Requests Complete";
       LinkButton buttonlink= (LinkButton)sender;
       buttonlink.Enabled = false;
   }
      mySubPanel.Update();
}

以下是从下拉列表中检索值的回发代码。下拉列表位于 Gridview 行内。

protected void LinkButtonAddFriend_Command(object sender, CommandEventArgs e)
{
    Guid RequestedId = new Guid(Membership.GetUser().ProviderUserKey.ToString());
    Guid UserId = new Guid(HiddenFieldUserId.Value.ToString());
    int UserSubjectId = Convert.ToInt32 (GridViewUserSubjects.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
    DropDownList DDL = (DropDownList)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownListMySubjectz");
    LinkButton RequestAction = (LinkButton)GridViewUserSubjects.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("LinkButtonFriendAction");
    int RequesterSubjectId = Convert.ToInt32(DDL.SelectedItem.Value);

    if (FriendsDataAccess.InsertRequest(UserId, RequestedId, UserSubjectId, RequesterSubjectId))
    {
        RequestAction.Text = "Remove Request";
        RequestAction.Enabled = true;
    }
}

DDL 是有麻烦的下拉菜单。

4

1 回答 1

1

你什么时候数据绑定GridView哪个是容器DropDownList?您是否已将其包装在if(!IsPostBack)-check 中?当您数据绑定时GridView,子控件总是“重置”。

如果您使用像这样的声明性数据绑定控件,则在不必要ObjectDataSource时应避免使用。this.databind()

于 2013-03-05T13:07:09.203 回答