0
votes

The gridview has multiple rows and columns and each cell has a textbox, validator controls, just like in Excel. The columns are generated dynamically, and I would like to clear all the textboxes.

This is not working. Where am I going wrong

protected void btnClear_Click(object sender, EventArgs e)
{
 if(gvMain.Rows.Count > 0)
  {
    foreach(GridViewRow gvr in gvMain.Rows)
     {
       foreach(TableCell tc in gvr.Cells)
        {
           if(tc.HasControls())
            {
              for(int i=0;i<tc.Controls.Count;i++)
               {
                 if(tc.Controls[i] is TextBox)
                   {
                     TextBox tb = (TextBox)tc.Controls[i];
                      tb.Text= "";
                   }
               }
            }
        }
     }
  }
}
4
Are you able to post the markup for gvMain? Also if you set a breakpoint on tb.Text= ""; does it actually get hit? - SLuck49
ah...its lengthy...and no it doesnt get hit. - Ruby
In that case you should try debugging through it and finding why not, it could be there are no controls in the cell, none of those controls are TextBoxes, etc... my guess is you'll see the cell does not contain your dynamically created controls. - SLuck49
Yes you are right. Though I can see the textboxes on page, it just doesnt get noticed in the loop. Guess I have to try something else. - Ruby
Basically your problem is that on postback the dynamic controls must be recreated. As long as you re-add the dynamic controls to each row in Page_Init during a postback ViewState should take care of filing in the controls properties (Text, Visible, etc). - SLuck49

4 Answers

2
votes

Here is the solution. I have already tried this and it worked fine.

     foreach (GridViewRow row in GridView1.Rows)
        {
            foreach (TableCell cell in row.Cells)
            {
                foreach (var control in cell.Controls)
                {
                    var box = control as TextBox;
                    if (box != null )
                    {
                        box.Text = string.Empty;
                    }
                }
            }
        }

Hope this will help

0
votes

Plz try below code:

foreach(GridViewRow r in GridView.Rows)
{
   TextBox txtbox = (TextBox)r.FindControl("Id of TextBox");
   txtbox.Text = "";
} 

Thanks

0
votes

Use below code:

 foreach(GridViewRow gr in GridView.Rows)
    {
       // find your all textbox
       TextBox txtA= (TextBox)gr.FindControl("txtA");
       TextBox txtB= (TextBox)gr.FindControl("txtB");
       //Check they are not null
       if(txtA!=null){
       //Assign empty string
           txtA.Text = string.empty;
           }
       if(txtB!=null){
       //Assign empty string
           txtB.Text = string.empty;
           }
    } 
0
votes

Basically the problem is that on postback the dynamic controls must be recreated. As long as you re-add the dynamic controls to each row in Page_Init during a postback ViewState should take care of filling in the controls properties (Text, Visible, etc).

Alternatively if you create the controls in RowCreated and then assign the default value in RowDataBound it will also keep the dynamic controls around. Here's a simplified version:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        gvTest.DataSource = new int[] { 1, 2, 3 };
        gvTest.DataBind();
    }
}

protected void gvTest_RowCreated(object sender, GridViewRowEventArgs e)
{
    //note: e.Row.DataItem will be null during postback
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].Controls.Add(new TextBox());
    }
}

protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        var txtBox = (TextBox)e.Row.Cells[0].Controls[0];
        txtBox.Text = e.Row.DataItemIndex.ToString();
    }
}

protected void Clear_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvTest.Rows)
    {
        var txtBox = (TextBox)row.Cells[0].Controls[0];
        //txtBox.Text will have whatever value the user gave it client side
        txtBox.Text = "";
    }
}