Find Control Inside Grid Row

im using the parent child grid and on the child grid that I am doing show / hide threw java script. and the child grid I bind runtime to Templatecolumns for example

GridView NewDg = new GridView(); NewDg.ID = "dgdStoreWiseMenuStock"; TemplateField TOTAL = new TemplateField(); TOTAL.HeaderTemplate = new BusinessLogic.GridViewTemplateTextBox(ListItemType.Header, "TOTAL",e.Row.RowIndex ); TOTAL.HeaderStyle.Width = Unit.Percentage(5.00); TOTAL.ItemTemplate = new BusinessLogic.GridViewTemplateTextBox(ListItemType.Item, "TOTAL", e.Row.RowIndex); NewDg.Columns.Add(TOTAL); NewDg.DataSource = ds; NewDg.DataBind(); NewDg.Columns[1].Visible = false; NewDg.Columns[2].Visible = false; System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw); NewDg.RenderControl(htw); 

Now I have one TextBox inside the Grid named "TOTAL". I want to find this text block and want to get its value.

How can I get this?

+4
source share
3 answers

You can get the TextBox control inside the corresponding GridView cell using the Controls or FindControl(string id) property:

 TextBox txtTotal = gv.Rows[index].cells[0].Controls[0] as TextBox; 

or

 TextBox txtTotal = gv.Rows[index].cells[0].Controls[0].FindControl("TOTAL") as TextBox; 

where index can be 0 for the first line or an iterator inside a for loop.

Alternatively, you can use the foreach loop over the rows of the GridView:

 foreach(GridViewRow row in gv.Rows) { TextBox txtTotal = row.cells[0].Controls[0].FindControl("TOTAL") as TextBox; string value = txtTotal.Text; // Do something with the textBox value } 

In addition, you should keep in mind that if you create a GridView dynamically (and not declaratively in a web form), you will not be able to get this control after the postback of the page.

The Rolla article has 4 great guys from the Rolls article: Dynamic Web Controls, Callbacks, and View State

+4
source

As you know, you can get the TextBox value, for example, for the first row and first cell:

 ((TextBox) dgdStoreWiseMenuStock.Rows[0].Cells[0].Controls[1]).Text; 

or change the index of control 0 if the above line does not work.

+2
source

try it

  TextBox txtTotal = (TextBox)gv.Rows[index].cells[0].FindControl("TOTAL"); string value = txtTotal.Text; 
+1
source

All Articles