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/>");
}
}
}
前台
复制代码 代码如下:
<%@ 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/>");
}
}
}
您可能感兴趣的文章:
- ASP.NET自定义Web服务器控件之Button控件
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- asp.net Page.EnableEventValidation 属性验证服务器控件的回发和回调事件出现的错误
- jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
- asp.net 服务器控件的 ID,ClientID,UniqueID 的区别
- asp.net下使用Request.From获取非服务器控件的值的方法
- jQuery生成asp.net服务器控件的代码
- ASP.NET 动态写入服务器端控件
- Asp.Net使用服务器控件Image/ImageButton显示本地图片的方法