首页 > 编程 > .NET > 正文

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

2024-07-10 13:21:34
字体:
来源:转载
供稿:网友
实例一:
前台

复制代码 代码如下:


<%@ 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 runat="server">
<div>
<asp:Button runat="server" Text="Button" />
</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>加上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/>");
}
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表