locked
gridview with dynamic dropdownlist control null exception RRS feed

  • Question

  • User-1114390349 posted

    I have a GridView with template fields and one of the template field is a dropdownlist which is dynamically added into the gridview control. It's working properly on page load when I click the button control to the get the values from the template fields it will throw a null reference exception. Please correct my error, I am in pathetic situation. I attached the source code.

    Regards

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            OnRowCreated="GridView1_RowCreated">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="id" runat="server" Text='<%# Eval("RGNO") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="name" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lastname" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


    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);
            }
        }
    
        private void BindGridView()
        {
            GridView1.DataSource = CreateDataSource();
            GridView1.DataBind();
        }
    
        public DataTable CreateDataSource()
        {
            DataTable dt = new DataTable();
            DataRow dr;
    
            dt.Columns.Add(new DataColumn("RGNO", typeof(string)));
            dt.Columns.Add(new DataColumn("NAME", typeof(string)));
            dt.Columns.Add(new DataColumn("DOB", typeof(string)));
    
            dr = dt.NewRow();
            dr["RGNO"] = "1001";
            dr["NAME"] = "PRAKASH";
            dr["DOB"] = "10-10-2003";
            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");
                Response.Write(dd1.SelectedItem.Text);
            }
        }
    Monday, November 11, 2019 3:05 PM

All replies

  • User283571144 posted

    Hi asolmdu,

    According to your codes, I found you directly find the control in the GridViewRow. Since you add the ddl into the PlaceHolder not the gridviewrow, you couldn't find the ddl control.

    The right way is you should find the PlaceHolder firstly, then find the ddl in it.

    More details ,you could refer to below codes:

    ASPX:

       <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            OnRowCreated="GridView1_RowCreated"  >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="id" runat="server" Text='<%# Eval("RGNO") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="name" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lastname" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
            <asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="value"/>
            </div>
    

    Code-behind:

          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);
                    //e.Row.Cells[3].Controls.Add(ddlResult);
                }
            }
    
            private void BindGridView()
            {
                GridView1.DataSource = CreateDataSource();
                GridView1.DataBind();
            }
    
            public DataTable CreateDataSource()
            {
                DataTable dt = new DataTable();
    
                DataRow dr;
                dt.Columns.Add(new DataColumn("RGNO", typeof(string)));
                dt.Columns.Add(new DataColumn("NAME", typeof(string)));
                dt.Columns.Add(new DataColumn("DOB", typeof(string)));
    
                dr = dt.NewRow();
                dr["RGNO"] = "1001";
                dr["NAME"] = "PRAKASH";
                dr["DOB"] = "10-10-2003";
                dt.Rows.Add(dr);
                return dt;
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    PlaceHolder dd1 = (PlaceHolder)row.FindControl("PlaceHolder1");
                    DropDownList dd2 = (DropDownList)dd1.Controls[0];
                    if (dd2 != null)
                    {
                        Response.Write(dd2.SelectedItem.Text);
                    }
                }
            } 
    

    The result:

    Best Regards,

    Brando

    Tuesday, November 12, 2019 7:33 AM