Karthick Saravana
Karthick Saravana

Reputation: 13

Cannot find control of dynamically generated textbox

Iam generating dynamic textbox in rowData bound as follows,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
       {  
           try  
           {  
               if (e.Row.RowType == DataControlRowType.DataRow)  
               { 
                   for (int colIndex = 0; colIndex < e.Row.Cells.Count; colIndex++)  
                   {  
                           int rowIndex = colIndex;
                           TextBox txtName = new TextBox();  
                           txtName.Width = 16; 
                           txtName.AutoPostBack = true;  
                           txtName.ID="txtName"
                           e.Row.Cells[colIndex].Controls.Add(txtName); 

                       }  
                   }  
               }  

           }  
           catch (Exception ex)  
           {  
           }  

       } 

on button click event i want to find control of that textbox as below,

foreach (GridViewRow rows in GridView1.Rows)
 {
      TextBox txtName= (TextBox)rows.FindControl("txtName");

      string test= txtName.Text;

 }

But above code return null. When I try to find control of dynamic textbox on button click event it returns null please help..

Upvotes: 0

Views: 130

Answers (1)

Fabius Wiesner
Fabius Wiesner

Reputation: 926

You have to set the name:

txtName.Name = "txtName";    // or txtName.Name = "whatever";

Upvotes: 2

Related Questions