查找控制网格行内 [英] Find Control inside Grid Row

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

问题描述

I M使用父子电网和电网儿童I M做显示/隐藏扔Java脚本。和儿童电网结合我运行时间Templatecolumns像

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

现在我已经内部电网一个TextBox命名为TOTAL我想找到这个文本框和想要得到它的价值。

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

怎样才能得到它?

推荐答案

您可以得到的GridView的相应小区内的TextBox控件,使用控件属性或的FindControl(字符串ID)方法:

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;

,其中索引可以为第一行,或者一个迭代内部的for循环是0

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

另外,你可以使用foreach循环在GridView的行:

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
}

另外,你要记住,如果你(在Web表单,而不是声明)动态创建GridView的,你将无法得到一个页面回发后此控件。

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家伙从罗拉的文章的主题:动态Web控件,回发和视图状态

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

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

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