asp.net Page.Controls对象(找到所有服务器控件)

实例一:
前台
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.ASPx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div id="div1">
<ASP:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>

后台
复制代码 代码如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string name = "tree";
//Server.Transfer("ajax.ASPx?id=1&name="+name);
ChangeControls();
}
/************Controls属性************
* this.Controls则包括所有控件。
* System.Web.UI.LiteralControl
System.Web.UI.HtmlControls.HtmlHead
System.Web.UI.LiteralControl
System.Web.UI.HtmlControls.HtmlForm
System.Web.UI.LiteralControl
* 为<div id="div1">加上runat属性,则Form.Controls里则找不到Button1
*/
private void ChangeControls()
{
foreach (System.Web.UI.Control control in this.Form.Controls)
{
if (control is Button)
{
Button btn = (Button)control;
btn.Text = "Hello";
}
}
foreach (Control control in this.Controls)
{
Response.Write(control.ToString() + "<br/>");
}
}
}

AspNet技术asp.net Page.Controls对象(找到所有服务器控件),转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。