Click here to Skip to main content
15,895,667 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"));
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");



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();
}



Any suggestion . thanks in advance..

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"));
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");


foreach (GridViewRow row in gvMark.Rows)
                {
DropDownList drpResult = (DropDownList)row.FindControl("ddlResult");
 
// Here "drpResult" i'm getting null

Result = drpResult.SelectedValue.ToString();
}
Posted
Updated 16-Jun-16 12:14pm
v2
Comments
F-ES Sitecore 7-Jun-16 8:32am    
I can't see any code when you add the dropdown to the gridview? Assuming you have just left that out, .net doesn't automatically remember controls you add yourself, you have to re-add all of your dynamic controls on every postback.

1 solution

You just created a DropDownList object but you never add it to the GridView cells actually. So there's really no way for you to access it on postbacks. Also you may need to create the control at RowCreated event instead. Here's a working code for your reference:

ASPX
ASP.NET
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" xmlns:asp="#unknown">
    <asp:gridview id="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
        <columns>
                <asp:templatefield>
                    <itemtemplate>
                        <asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
                        </itemtemplate>
                </asp:templatefield>
         </columns>
    </asp:gridview>
    <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" />
</asp:content>


CODE BEHIND:

C#
using System;
using System.Web.UI.WebControls;
using System.Data;

namespace WebFormDemo
{
    public partial class DynamicControlInGridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack)
                BindGridView();
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                DropDownList ddlResult = new DropDownList();
                ddlResult.ID = "ddlResult";
                ddlResult.Items.Insert(0, new ListItem("PASS", "0"));
                ddlResult.Items.Insert(1, new ListItem("FAIL", "1"));

                PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
                p.Controls.Add(ddlResult);

                //you could also add it to the cells collection like
                TextBox tbox = new TextBox();
                tbox.ID = "TextBox1";
                e.Row.Cells[0].Controls.Add(tbox);

            }
        }

        private void BindGridView() {
            GridView1.DataSource = CreateDataSource();
            GridView1.DataBind();
        }

        public DataTable CreateDataSource() {
            DataTable dt = new DataTable();
            DataRow dr;

            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Lastname", typeof(string)));

            dr = dt.NewRow();
            //add values to each columns
            dr["ID"] = 1;
            dr["Name"] = "Vincent";
            dr["LastName"] = "Durano";
            dt.Rows.Add(dr);
            return dt;
        }

        protected void Button1_Click(object sender, EventArgs e) {
            foreach (GridViewRow row in GridView1.Rows) {
                DropDownList dd1 = (DropDownList)row.FindControl("ddlResult");
                if (dd1 != null) {
                    Response.Write("Found DropDown!");
                }

                TextBox tbox = row.FindControl("TextBox1") as TextBox;
                if (tbox != null) {
                    Response.Write("Found TextBox!");
                }
            }
           
        }
       
    }
}
 
Share this answer
 

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