I have a Gridview having 3 columns in which i took checkbox,label and label as item templates for columns select,code,name respectively.
ecode column is not editable.Button edit and update are present outside of gridview.

i can edit multiple rows like this...

when i select checkbox in 1st row of gridview then in ename column of that row i added textbox by this code

protected void btnEdit_Click(object sender, EventArgs e)
    {
        
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            txteditEname = new TextBox();       
            CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
            Label lblEname = (Label)GridView1.Rows[i].Cells[0].FindControl("lblEname");
           
            if (chkDelete.Checked)
            {
                GridView1.Rows[i].Cells[2].Controls.Add(txteditEname);
                txteditEname.Text = lblEname.Text;
                lblEname.Visible = false;
            }

        }
    }

protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ArrayList al = new ArrayList();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
            txteditEname = (TextBox)GridView1.Rows[i].Cells[2].FindControl("txteditEname");
            string str=txteditEname.Text;//error

//here i set breakpoint and wanted to retrieve txteditEname but error comes...
then thinking that there is no such textbox when i press Update button as autopost back then
i saved the txteditEname 's value in Viewstate but when i retrieved viewstate it showed me the textbox's value before page is posted back.
But the current txteditEname's value is different which i want to update.Please tell me how to get curret txteditEname's value.
But before that i wonder if this textbox is not available then i'll surely get null value.

Dani AI

Generated

Short diagnosis and the fix in a nutshell: the TextBox you add in btnEdit_Click exists only for that request. On the next postback ASP.NET recreates the page/control tree from scratch, so any dynamic control must be recreated with the same ID early in the life cycle (before LoadViewState) for its posted value to be restored. Also, if you never set TextBox.ID you cannot find it later with FindControl.

This ties to what said about the page life cycle and to ’s RowDataBound idea. Both are useful: recreate the dynamic TextBox on every postback (not only on the Edit click) and give it a deterministic ID. Two practical approaches:

  • Easiest: put a TextBox in the ItemTemplate (hidden by default) and toggle visibility when the row is selected. No dynamic creation, no lifecycle headaches.
  • If you must create controls dynamically: when Edit is clicked save the selected rows’ keys (DataKey or row index) in ViewState. On the next request rebind the GridView early (PageInit / OnInit) and add the TextBox for each saved key inside RowDataBound or RowCreated, setting `ID = "txteditEname" + key. Because the grid is rebuilt before LoadViewState, ASP.NET will populate the TextBox.Text with the posted value andFindControl("txteditEname_" + key)` will return the current value in your Update handler.

Common pitfalls and tips:

  • Don’t unconditionally rebind the GridView in Page_Load (use if (!IsPostBack) unless you intentionally rebind in Init for dynamic controls).
  • Do not rely on manually saving the current TextBox.Text into ViewState before postback — that will give you the previous value, not the one the user just typed.
  • Use DataKeys (or a stable unique ID) rather than row index if rows can be reordered or paged.
  • Prefer generic collections (List<T>) over ArrayList.

Example pattern (rebind in Init, create controls in RowDataBound with deterministic IDs, read them in btnUpdate) will solve the null/old-value problems.

Recommended Answers

All 2 Replies

Please refer the ASP.NET Page life cycle topic at MSDN. ASP.NET page and control objects are created on each post back.

your code will never work..

you need to iterate through Gridview rows..

Checking should be done on RowDataBound Event..

foreach(GridviewRows Gvr in Gridview1.rows)
{
  Checkbox chk = (CheckBox)e.row.FindControl("Name Of Your Checkbox");

if(chk.checked == true)
{
 // carry out rest of the operation...
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.