Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created dynamic dropdownlist in gridview rowdatabound pls refer below code.

C#
DropDownList ddlResult = new DropDownList();
                    ddlResult.ID = "ddlResult";
                    ddlResult.AutoPostBack = true;
                    ddlResult.SelectedIndexChanged += ddlResult_SelectedIndexChanged; 
                    ddlResult.Items.Insert(0, new ListItem("PASS", "0"));
                    ddlResult.Items.Insert(1, new ListItem("FAIL", "1"));



I've a button below the gridview, when i click the button i wants to access and get the above dropdown selected value. like below code inside click event

C#
foreach (GridViewRow row in gvMark.Rows)
                {
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");

// Here "drpResult" i'm getting null

Result = drpResult.SelectedValue.ToString();


What I have tried:

C#
DropDownList ddlResult = new DropDownList();
                    ddlResult.ID = "ddlResult";
                    ddlResult.AutoPostBack = true;
                    ddlResult.SelectedIndexChanged += ddlResult_SelectedIndexChanged; 
                    ddlResult.Items.Insert(0, new ListItem("PASS", "0"));
                    ddlResult.Items.Insert(1, new ListItem("FAIL", "1"));




C#
foreach (GridViewRow row in gvMark.Rows)
                {
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");

// Here "drpResult" i'm getting null

Result = drpResult.SelectedValue.ToString();
Posted
Updated 6-Jun-16 5:34am

1 solution

As far as I can see you never add the DropDownList control in the row in the first place. Try adding the drop down list into the controls collection of the cell in GridView.RowCreated Event (System.Web.UI.WebControls)[^].

In other words
C#
e.Row.Cells[colnumber].Controls.Add(ddlResult);
 
Share this answer
 
Comments
rajah rajah 6-Jun-16 23:32pm    
Hi Mika,

Sorry i forgot to mention it.I have added the control like below

e.Row.Cells[i].Controls.Add(ddlResult);

I'm getting DDL in UI I just want to get the selected value of each row's DDL while clicking on button click event.Below code is inside the button click event

foreach (GridViewRow row in gvMark.Rows)
{
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");

// Here "drpResult" i'm getting null

Result = drpResult.SelectedValue.ToString();
}

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