Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i insert textbox in existing gridview by the code below on row databound event
Quote:
TextBox tx = new TextBox();
tx.ID = "txtStore";
tx.Attributes.Add("runat", "server");
e.Row.Cells[2].Controls.Add(tx);

now when i am trying to find and read the value of this text box is raise null exception code is -
Quote:
for (int i = 0; i < GvVarientstoredetails.Rows.Count; i++)
{
TextBox box1 = (TextBox)GvVarientstoredetails.Rows[i].Cells[2].FindControl("txtStore");
string s = box1.Text;
}


What I have tried:

for (int i = 0; i < GvVarientstoredetails.Rows.Count; i++)
{
TextBox box1 = (TextBox)GvVarientstoredetails.Rows[i].Cells[2].FindControl("txtStore");
string s = box1.Text;
}
string CustomerID = ((TextBox)GvVarientstoredetails.Rows[0].FindControl("txtStore")).Text;

please guide...
Posted
Updated 4-Aug-16 5:32am

1 solution

I haven't seen your whole code in RowDataBound event so I can't tell what went wrong. You might want to use the RowCreated event to ensure that the controls will be created. Here's a quick example:

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) {
                TextBox tbox = new TextBox();
                tbox.ID = "TextBox1";
 
                PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
                p.Controls.Add(tbox);
 
                //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) {
	    //access TextBox controls
            foreach (GridViewRow row in GridView1.Rows) {
                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