Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have dropdownlist within Gridview. When I rebind/Refresh the gridview then it reset the selected value.

Please help me. How can I solve this issue
Thanx
Seema
Posted

Hello Seema,

If you refresh or rebind the GridView, it will everytime reset the selected value. If you want to retain it, just check what are the items you need to update rather than refreshing the whole data.
 
Share this answer
 
XML
hi seema,
I write some codes:

In Html Page:

<asp:gridview runat="server" id="GridView1" autogeneratecolumns="false" cellpadding="3" cellspacing="0" xmlns:asp="#unknown">
    <columns>
      <asp:templatefield headertext="Column1">
        <itemtemplate>
           <asp:dropdownlist runat="server" id="DropDownList1" width="150px/">
          <asp:hiddenfield runat="server" id="HiddenField1" value="<%#Bind("Column1") %"> />
        </asp:hiddenfield></asp:dropdownlist></itemtemplate>
      </asp:templatefield>
      <asp:boundfield datafield="Column2" headertext="Column2" />
      <asp:boundfield datafield="Column3" headertext="Column3" />
    </columns>
   </asp:gridview>

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            DataTable datatable = new DataTable();
            datatable.Columns.Add("Column1", typeof(System.String));
            datatable.Columns.Add("Column2", typeof(System.String));
            datatable.Columns.Add("Column3", typeof(System.String));

            datatable.Rows.Add("1", "2", "3");
            datatable.Rows.Add("4", "5", "6");

            this.GridView1.DataSource = datatable;
            this.GridView1.DataBind();

            foreach (GridViewRow GridViewRow1 in this.GridView1.Rows)
            {
                //find hiddenField and DropDownList by id
                HiddenField hiddenField1 = (HiddenField)GridViewRow1.FindControl("HiddenField1");
                DropDownList dropDownList1 = (DropDownList)GridViewRow1.FindControl("DropDownList1");
                if (hiddenField1 != null && dropDownList1 != null)
                {
                    dropDownList1.Items.Clear;
                    //bind dropDownList1
                    //.....
                    dropDownList1.SelectedValue = hiddenField1.Value;
                }
            }
        }


Hope make you understand.
 
Share this answer
 
Comments
Seema Gosain 8-Jul-10 13:50pm    
Reason for my vote of 5
This answer helps me to solve dropdownlist postback.

Thank you very much
Helen.Yu 13-Jul-10 5:05am    
I glad to help u.
save data in ViewState. and bind it after rebind/Refresh on your DropDownList.
 
Share this answer
 
Have you trying by putting the gridView in
<asp:updatepanel xmlns:asp="#unknown" />
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900