Click here to Skip to main content
14,733,886 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all,

I have added a new control in Rowdatabound event of Gridview .Below is the code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       if (e.Row.RowType == DataControlRowType.DataRow)
           DataRowView drv = (DataRowView)e.Row.DataItem;
           if (drv != null)
               Literal ltr = e.Row.FindControl("ltr") as Literal;
               ltr.Text = drv.Row["Id"].ToString();
               if (e.Row.RowIndex == 0)
                   TextBox oTB = new TextBox();
                   oTB.ID = "TextBox1";
                   oTB = null;
               if (e.Row.RowIndex == 1)
                   Panel p1 = new Panel();
                   p1.Width = Unit.Pixel(500);
                   p1.Height = Unit.Pixel(200);
                   p1.ScrollBars = ScrollBars.Vertical;
                   CheckBoxList cbl = new CheckBoxList();
                   cbl.ID = "cbl";
                   cbl.RepeatColumns = 2;
                   cbl.Items.Add(new ListItem("JAKARTA", "JKT"));
                   cbl.Items.Add(new ListItem("Maharashtra", "MH"));

I wrote below code to access control but it always give null,
foreach (GridViewRow row in GridView1.Rows)
           TextBox txt = row.FindControl("TextBox1") as TextBox;
           CheckBoxList cbl = row.FindControl("cbl") as CheckBoxList;   

How do i access the control?
Please Help

[edit]Code blocks added - OriginalGriff[/edit]
Updated 4-Jun-11 3:36am

1 solution

You can't, unless your controls are added before the page load event, and added the same way every time. Otherwise they do not exist in time, in which case their viewstate is never restored. If your code is running in page load, to access the controls, that is why they don't exist, instead of being empty. Page load runs before data bound events, prerender runs after.
Member 7666785 4-Jun-11 8:48am
So what can i do in my case?
Is there any other way of doing it?
Christian Graus 4-Jun-11 10:57am
You can always have the controls there and just toggle if they are visible. You can write javascript to store the data of your textboxes, with IDs, in a hidden control. Those are the most obvious solutions.

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