C# 在网格行内查找控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1965835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Control inside Grid Row
提问by user239146
i m using parent child grid and on child grid i m doing Show / hide threw java script. and child grid i bind run time with Templatecolumns like
我使用父子网格和子网格我在做显示/隐藏抛出的java脚本。和子网格我将运行时与 Templatecolumns 绑定在一起
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 Grid named "TOTAL" I want to Find This TextBox and wanna get its value.
现在我在 Grid 中有一个名为“TOTAL”的 TextBox,我想找到这个 TextBox 并获取它的值。
How can Get it ?
怎样才能得到呢?
回答by Florim Maxhuni
As you know you may get TextBox value for example for the first row and the first cell:
如您所知,您可能会获得例如第一行和第一个单元格的 TextBox 值:
((TextBox) dgdStoreWiseMenuStock.Rows[0].Cells[0].Controls[1]).Text;
or change the index of control put 0 if the above line dosen't work.
或者如果上面的行不起作用,则更改控件的索引为 0。
回答by alexphi
You could get the TextBox control inside the corresponding cell of the GridView, using the Controlsproperty or the FindControl(string id)method:
您可以使用Controls属性或FindControl(string id)方法在 GridView 的相应单元格内获取 TextBox 控件:
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 could be 0 for the first row, or an iterator inside a for loop.
其中第一行的 index 可以是 0,也可以是 for 循环内的迭代器。
Alternatively, you can use a foreach loop over the GridView's rows:
或者,您可以在 GridView 的行上使用 foreach 循环:
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's value
}
Besides, you have to keep in mind that, if you're creating the GridView dynamically (and not declaratively in the web form), you won't be able to get this control after a page postback.
此外,您必须记住,如果您动态创建 GridView(而不是在 Web 表单中声明),您将无法在页面回发后获得此控件。
There is a great 4 Guys from Rollaarticle on the subject: Dynamic Web Controls, Postbacks, and View State
Rolla 有一篇很棒的4 Guys关于这个主题的文章:Dynamic Web Controls、Postbacks 和 View State
回答by Kushagra Bansal
Try This
尝试这个
TextBox txtTotal = (TextBox)gv.Rows[index].cells[0].FindControl("TOTAL");
string value = txtTotal.Text;
回答by Manali
The easiest way to find the control inside the gridview use foreach loop to find the rows
在gridview中查找控件的最简单方法使用foreach循环查找行
foreach (GridViewRow row in Gridname.Rows)
{
TextBox txttotal = (TextBox)row.FindControl("textboxid_inside_grid");
string var = txttotal.Text;
Response.Write("Textbox value = " + var);
}

