Dynamically adding value in drop down list of a Gridview
-
Hi, I have a gridview which has dropdown list in template column and i am adding value dynamically to that drop down list.Now I want to save all the values which I have selected in the dropdown list. But while clicking on save button in that page it is not retaining the selected values of the ddl. How can I solve this?
Thanks & Regards Mishra
-
Hi, I have a gridview which has dropdown list in template column and i am adding value dynamically to that drop down list.Now I want to save all the values which I have selected in the dropdown list. But while clicking on save button in that page it is not retaining the selected values of the ddl. How can I solve this?
Thanks & Regards Mishra
How are you adding the items? Through Javascript? If so, your server-side code knows nothing about the items. When the page is reloaded after a postback the DDL will be reloaded from the server-side. Any items added using client-side code will be lost. You would either need to post back the details for the items added in client-script to your server, or delegate the loading of the DDL to your client-side code.
Paul Marfleet
-
How are you adding the items? Through Javascript? If so, your server-side code knows nothing about the items. When the page is reloaded after a postback the DDL will be reloaded from the server-side. Any items added using client-side code will be lost. You would either need to post back the details for the items added in client-script to your server, or delegate the loading of the DDL to your client-side code.
Paul Marfleet
Hi, I am adding values to the ddl in RowDataBound event using the following code. if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)e.Row.FindControl("ddlvalidation"); if (lblcheck.Text == "Suspect") { ddlval.Items.Add("Error"); ddlval.Items.Add("No Error"); } else if (lblcheck.Text == "Fatal") { ddlval.Items.Add("Not Fixed"); ddlval.Items.Add("Fixed"); } & In the button save I am trying to fetch the selected values using the following code, but I am getting the error "Object reference not set to an instance of an object." foreach (GridViewRow rec in this.gvsqldesc.Rows) { //Response.Write(rec.Cells[0].Text); DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)rec.FindControl("ddlvalidation"); string str = null; str = ddlval.SelectedItem.Text; }
Thanks & Regards Mishra
-
Hi, I am adding values to the ddl in RowDataBound event using the following code. if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)e.Row.FindControl("ddlvalidation"); if (lblcheck.Text == "Suspect") { ddlval.Items.Add("Error"); ddlval.Items.Add("No Error"); } else if (lblcheck.Text == "Fatal") { ddlval.Items.Add("Not Fixed"); ddlval.Items.Add("Fixed"); } & In the button save I am trying to fetch the selected values using the following code, but I am getting the error "Object reference not set to an instance of an object." foreach (GridViewRow rec in this.gvsqldesc.Rows) { //Response.Write(rec.Cells[0].Text); DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)rec.FindControl("ddlvalidation"); string str = null; str = ddlval.SelectedItem.Text; }
Thanks & Regards Mishra
Your button click event fires before the grid databinding has taken place. The controls you had added dynamically have not been recreated yet. You should wait until the grid databinding has taken place before attempting to reference any controls you have created dynamically.
Paul Marfleet