ASP.net page.controls object (Find all server controls) _ Practical Tips

Source: Internet
Author: User
Example One:
Front desk
Copy Code code as follows:

<%@ 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 ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div id= "Div1" >
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
</div>
</form>
</body>

Background
Copy Code code as follows:

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 Property ************
* this. Controls includes all the controls.
* System.Web.UI.LiteralControl
System.Web.UI.HtmlControls.HtmlHead
System.Web.UI.LiteralControl
System.Web.UI.HtmlControls.HtmlForm
System.Web.UI.LiteralControl
* For <div id= "Div1" > plus runat attribute, form.controls is not found in 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-this. Controls)
{
Response.Write (Control. ToString () + "<br/>");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.