DropDownList-Items are null on postback

I have DropDownList that I have to populate in an event associated with a click event of another control.. the data is populated and present but when I select a value and postback the values are null. Which means the view state is not working.. Solutions say that populate the DropDown in Init() but I can’t because the requirement does not allow this. I have to populate it on click event .. Viewstates are enabled.. The following code populates the 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();
}

and the following is the code for the postback that retrieves the value from the Dropdown list.. The DropDown is inside a 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 is the Dropdown with trouble.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

When are you databinding the GridView which is the container of the DropDownList? Do you have wrapped it in a if(!IsPostBack)-check? When you databind the GridView the child controls are always “resetted”.

If you use a declarative databound control like ObjectDataSource you should avoid this.databind() if it’s not necessary.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply