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 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/>");
}
}
}

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjfdwx.html