欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-04 23:02:04
实例一: 前台 复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codefile="default.a...
实例一:
前台
复制代码 代码如下:

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