DropDownList-Items在回发时为null

时间:2013-03-04 13:30:48

标签: asp.net drop-down-menu

我有DropDownList,我必须在与另一个控件的click事件相关联的事件中填充..数据被填充并存在但是当我选择一个值并且回发时,值为null。这意味着视图状态不起作用。解决方案说在Init()中填充DropDown但我不能,因为要求不允许这样做。我必须在点击事件上填充它..查看状态已启用..以下代码填充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();
}

以下是从Dropdown列表中检索值的回发代码.DropDown位于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是有问题的下拉列表。

1 个答案:

答案 0 :(得分:1)

您何时对GridView的容器DropDownList进行数据绑定?你把它包裹在if(!IsPostBack) - 检查?当您对GridView进行数据绑定时,子控件始终会“重置”。

如果使用ObjectDataSource之类的声明性数据绑定控件,如果没有必要,则应避免使用this.databind()