使用母版页后,如何遍历内容页里的控件?

hb9191 2009-09-14 04:05:55
使用母版页后,如何遍历内容页里的控件?
例如:找出 TextBox 或 ID="XX"
...全文
165 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengjiangwen 2009-09-14
  • 打赏
  • 举报
回复
可以通过强引用

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>//在母版页源码中加入这句
然后可以在母版页中定义公共属性或方法
public string GetUserName()
{
return Page.User.Identity.Name;//返回控件的内容等
}
在内容页中调用
Label1.Text = "欢迎光临" + Master.GetUserName();
wuyq11 2009-09-14
  • 打赏
  • 举报
回复
private void GetControls(Control c)
{
if (c is TextBox)
{
TextBox txt = (TextBox)control;
}
if (c.HasControls())
{
foreach (Control con in c.Controls)
GetControls(con);
}
}
在母版页
Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1") as TextBox;
puzhichen 2009-09-14
  • 打赏
  • 举报
回复

public static Control IterateThroughControl(Control parent)
{
Control control = null;
foreach (Control c in parent.Controls)
{
if (c.GetType() == typeof(TextBox))
{
control = c;
return control;
}
if (c.HasControls()) // 判断该控件是否有下属控件。
{
control = IterateThroughControl(c); //递归,访问该控件的下属控件集。
if (c.GetType() == typeof(TextBox))
return control;
}
}
return control;
}
Lovely_baby 2009-09-14
  • 打赏
  • 举报
回复
怎么你的方法不行吗??
hb9191 2009-09-14
  • 打赏
  • 举报
回复
我现在是这样:


foreach (Control c in this.Form.Controls)
{
if (c.GetType().Name=="ContentPlaceHolder")
{
foreach (Control c1 in c.Controls)
{
if c1....
}
}
}
puzhichen 2009-09-14
  • 打赏
  • 举报
回复
我也碰到了楼主一样的问题,但是魅力值不够,提问了没多少人回答,认为这是低级中的低级.
汗倒!把希望寄托在楼主的魅力值上!(强烈建议你去换个美女头像,保证回帖数飙升).
watsonchia 2009-09-14
  • 打赏
  • 举报
回复
内容页里的控件用平常的方法就可以,母版里的控件可以用master.findcontrol

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧