在网格行中查找控件 [英] Find Control inside Grid Row

查看:27
本文介绍了在网格行中查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用父子网格,而在子网格上我正在执行显示/隐藏抛出的 Java 脚本.和子网格我将运行时与模板列绑定,如

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

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);

现在我在 Grid 中有一个名为TOTAL"的 TextBox,我想找到这个 TextBox 并获取它的值.

Now I have one TextBox inside Grid named "TOTAL" I want to Find This TextBox and wanna get its value.

如何获得?

推荐答案

您可以使用 Controls 属性或 FindControl(stringid) 方法:

You could get the TextBox control inside the corresponding cell of the GridView, using the Controls property or the FindControl(string id) method:

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

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

其中第一行的 index 可以是 0,也可以是 for 循环中的迭代器.

where index could be 0 for the first row, or an iterator inside a for loop.

或者,您可以在 GridView 的行上使用 foreach 循环:

Alternatively, you can use a foreach loop over the GridView's rows:

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
}

此外,您必须记住,如果您动态创建 GridView(而不是在 Web 表单中声明),您将无法在页面回发后获得此控件.

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.

有一篇很棒的 4 Guys from Rolla 关于这个主题的文章:动态 Web 控件、回传和视图状态

There is a great 4 Guys from Rolla article on the subject: Dynamic Web Controls, Postbacks, and View State

这篇关于在网格行中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆