2013-03-04 61 views
0

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

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在Gridview Row中。

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是有问题的下拉菜单。

+2

编辑问题标题 - 下拉式拼写错误 – 2013-03-04 13:32:58

+0

编辑过我可否有建议? – spiral 2013-03-04 13:37:09

回答

1

什么时候数据绑定GridView这是DropDownList的容器?你是否用if(!IsPostBack)包装它 - 检查?当您绑定GridView时,子控件始终“重置”。

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

+0

绝对是......我决定暂时不要使用this.databind()。这是我整整一周浪费的第二次。谢谢蒂姆。我解脱了很多,我说不出来。 – spiral 2013-03-05 13:12:14