C#在控件内部查找控件 [英] C# Find controls inside the controls

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

问题描述

我有一个gridveiw,其中itemtemplate里面有Panal,而Panal包含其他服务器控件.

我想使用此Panle和服务器控件,并使用C#更改其文本和其他值,但是我无法在Panal旁边找到控件.

I have a gridveiw in which there is panal inside itemtemplate and the panal contains other server control.

i want to use this panle in and server controls and change their text and other values using C# but i can''t find the controls in side the panal.

推荐答案

尝试YourPanelName.FindControl()

看看类似的线程:
在ASP.NET面板中查找所有控件 [ ^ ]
如何找到放置的控件 [ ^ ]

..和更多线程
Try YourPanelName.FindControl()

Look on similar threads:
Finding all controls in an ASP.NET Panel[^]
How do I find a control I placed inside a panel?[^]

..and more threads here[^]


要在面板中查找控件,可以使用"Controls"属性.参见以下代码:
For finding controls inside the Panel you may use the "Controls" property. See this code:
foreach(Control ctrl in panel1.Controls)
{
  ctrl.Text = "Hello";
}


由于面板位于项目模板内部,因此将在网格视图中的每一行重复该面板.

因此,首先您必须进入要更改控件文本的行.

例如GridViewRow gr = Gridview1.Rows [0];//取第一行

然后您可以找到以下面板:

面板tempPanel =(Panel)gr.FindControl("PanelID");

然后您可以使用
在面板内找到文本框
TextBox txt =(TextBox)tempPanel.FindControl("TextBoxID");

或者您可以在网格视图行内直接找到该文本框(如果文本框ID在页面中是唯一的),如下所示:

TextBox txt =(TextBox)gr.FindControl("TextBoxID");


现在,您可以将控件的文本更改为:txt.Text ="New Value";


希望这对您有帮助!
As the panel is inside the item template, it will be repeated for each row in grid view.

So first you have to take the row in which you want to change control''s text.

e.g. GridViewRow gr = Gridview1.Rows[0];//taking the first row

Then you can find the panel as follows :

Panel tempPanel = (Panel)gr.FindControl("PanelID");

And then you may find the textbox inside the panel using

TextBox txt = (TextBox)tempPanel.FindControl("TextBoxID");

Or you may find this textbox directly inside the grid view row (if the textbox id is unique in the page) as follows:

TextBox txt = (TextBox)gr.FindControl("TextBoxID");


Now you can change the text of the control as : txt.Text = "New Value";


Hope this helps you !!


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

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