foreach (Control control in page.Controls)
{
GetControls(control, language);
}public static void GetControls(Control control,string language)
{
if (control.HasControls())
{
foreach (Control childc in control.Controls)
{
GetControls(childc, language);
}
}}
再问一个问题:我在遍历获取页面所有控件时,不能获取UpdatePanel里面的控件。如果不用UpdatePanel就都能可以获取到,
请问有没有好的解决方法呀?怎样或UpdatePanel里面的控件?
读到UpdatePanel时control.HasControls() 判断为false,所以.....
{
GetControls(control, language);
}public static void GetControls(Control control,string language)
{
if (control.HasControls())
{
foreach (Control childc in control.Controls)
{
GetControls(childc, language);
}
}}
再问一个问题:我在遍历获取页面所有控件时,不能获取UpdatePanel里面的控件。如果不用UpdatePanel就都能可以获取到,
请问有没有好的解决方法呀?怎样或UpdatePanel里面的控件?
读到UpdatePanel时control.HasControls() 判断为false,所以.....
解决方案 »
- 怎么实现电脑10钟无操作自动关机?
- DataGridView分页的基础问题
- SQL
- 各位高手,请问做我用C#做B/S结构,要注意哪些关键问题?
- 关于socket通信问题,为什么这段程序会有问题
- 正则表达式如何删除部分 <img中的信息
- C#的程序里用到的图标资源的路径可以修改吗?
- 绝对有深度的问题.关于System.Runtime.InteropServices.UCOMIStream的使用.
- 寻Microsoft Visual Studio Tools for the Microsoft Office System中文版
- 使用c#做的即时通讯软件,udp和tcp可以混合使用么?如果能的话需要注意什么东西?
- 我想请哪位帮我把这段java代码用c#重写一遍,谢谢
- DataGridView DataGridViewComboBoxColumn列的clear方法无效!
<ContentTemplate>
提交刷新用的.
因此如果你想遍历UpdatePanel控件里面的控件,先遍历到UpdatePanel自己,然后可以调用它的FindControl()方法通过Id名称来找到指定的控件,或者通过Controls[]属性,给下标索引的方式去找或者遍历控件。
{ foreach (Control control in page.Controls)
{
GetControls(control, language);
}
}
public static void GetControls(Control control,string language)
{
string oldText = "";
common.Logger.LogNote("NamingContainer:" + control.NamingContainer.ToString());
common.Logger.LogNote("ID:" + control.ID); if (control.ID == "updatePanel1")
{
newv.common.Logger.LogNote("ID:" + control.ID);
}
Type controlType = control.GetType();
switch (controlType.ToString())
{
case "System.Web.UI.WebControls.TextBox":
oldText = ((TextBox)control).Text.ToString();
if (!string.IsNullOrEmpty(oldText))
((TextBox)control).Text = Translate(oldText, language, oldText);
break;
case "System.Web.UI.WebControls.Label":
oldText = ((Label)control).Text.ToString();
if (!string.IsNullOrEmpty(oldText))
((Label)control).Text = Translate(oldText, language, oldText);
break;
case "System.Web.UI.WebControls.Literal":
oldText = ((Literal)control).Text.ToString();
if (!string.IsNullOrEmpty(oldText))
((Literal)control).Text = Translate(oldText, language, oldText);
common.Logger.LogNote("Literal:" + oldText.ToString());
break;
case "System.Web.UI.WebControls.LiteralControl":
oldText = ((LiteralControl)control).Text.ToString();
if (!string.IsNullOrEmpty(oldText))
((LiteralControl)control).Text = Translate(oldText, language, oldText);
common.Logger.LogNote("LiteralControl:" + oldText.ToString());
break;
case "System.Web.UI.HtmlControls.HtmlInputButton":
oldText = ((HtmlInputButton)control).Value.ToString();
if (!string.IsNullOrEmpty(oldText))
((HtmlInputButton)control).Value = Translate(oldText, language, oldText);
common.Logger.LogNote("HtmlInputButton:" + oldText.ToString());
break;
default:
common.Logger.LogNote("default:" + controlType.ToString());
break;
} if (control.HasControls())
{
foreach (Control childc in control.Controls)
{
GetControls(childc, language);
}
}
}
如果让你用上面的方法遍历控件的时候得到GridView中的控件你可以做到吗?显然不能。
同样的道理,UpdatePanel控件和GridView控件时类似的,而不是和panel类似的。
也就是要先找到UpdatePanel的ContentTemplate,然后用HasControls判断,因为控件是在ContentTemplate模板中的,而不是在UpdatePanel中的。
----------
UpdatePanel我没有实际用过,以上只是推测。
----------
参考:
ContentTemplate 属性包含呈现时显示在 UpdatePanel 控件内部的内容。可以以声明方式或编程方式创建模板的内容。UpdatePanel 控件的内容模板是单实例模板。因此,无需使用 FindControl 方法就可以直接在页上访问模板中所包含控件的属性。有关更多信息,请参见 TemplateInstanceAttribute 类的概述。 若要以声明方式将项添加到 UpdatePanel 控件中,请在 UpdatePanel 控件的开始标记和结束标记之间添加 <ContentTemplate> 元素。在 <ContentTemplate> 元素标记内部,声明其他控件和 HTML 元素。
((System.Web.UI.UpdatePanel)(control)).ContentTemplate
找到下面的方法怎么写呢?还是没取出来。能否具体一下
下面是我遍历我的一个Id为UpdatePannel1的UpdatePannel得控件的例子,如果里面有TextBox,我让它里面显示"Hello!"。foreach (Control c in UpdatePanel1.ContentTemplateContainer.FindControl.Controls)
{
if (c is System.Web.UI.WebControls.TextBox)
{
((System.Web.UI.WebControls.TextBox)c).Text = "Hello!";
}
}这下应该没错了,希望对楼主有帮助!
{
if (c is System.Web.UI.WebControls.TextBox)
{
((System.Web.UI.WebControls.TextBox)c).Text = "Hello!";
}
}
出错信息:Cannot instantiate the ContentTemplate in the Init event when the ContentTemplateContainer was already created manually in UpdatePanel with ID 'updatePanel1'.
出错跟踪信息: at System.Web.UI.UpdatePanel.CreateContents(Boolean recreate)
at System.Web.UI.UpdatePanel.OnInit(EventArgs e)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
try
{
if (control.GetType() == typeof(UpdatePanel))
{
int i = 0;
foreach (Control c in ((UpdatePanel)control).ContentTemplateContainer.Controls)
{
if (c is System.Web.UI.WebControls.Literal)
{
oldText = ((System.Web.UI.WebControls.Literal)c).Text;
newv.common.Logger.LogNote("Literal:" + oldText.ToString());
}
i++;
newv.common.Logger.LogNote("UpdatePanel:" + i.ToString());
}
}