How to Set a property of all controls on the page

Source: Internet
Author: User

For setting the properties of all controls on the page, you need to use recursive methods for search, and you can use reflection to determine the attributes and set the attribute values. The Code is as follows:

ASPX code
<% @ Page Language = "C #" AutoEventWireup = "true" %>
<% @ Import Namespace = "System. Reflection" %>
<% @ Import Namespace = "System. ComponentModel" %>
<! 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">
<Script runat = "server">
Protected void button#click (object sender, EventArgs e)
{
Setcontrolarrti.pdf (Page );
}

Private void setcontrolarrti.pdf (Control ctl)
{
Type type = ctl. GetType ();
PropertyInfo [] properties = type. GetProperties ();
Response. Write ("Response. Write (type. ToString ());
Response. Write ("<br> ");
Foreach (PropertyInfo property in properties)
{
// Not all attributes can be obtained through this method?
If (property. Name = "Text" & property. PropertyType = typeof (String ))
{
Response. Write ("<li>" + property. Name + "=" + GetPropertyValue (ctl, property. Name ));
}
If (property. Name. Equals ("Enabled "))
{
Type propertyType = property. PropertyType;
TypeConverter converter = TypeDescriptor. GetConverter (propertyType );
Object arg = converter. ConvertFrom ("false ");
Object [] args = {arg };
BindingFlags flags = BindingFlags. SetProperty;
Binder binder = null;
Type. InvokeMember ("Enabled", flags, binder, ctl, args );
}
}

If (ctl. HasControls ())
{
Foreach (Control c in ctl. Controls)
{
SetControlArrtibute (c );
}
}
}

Private String GetPropertyValue (Control control, String propertyName)
{
Type type = control. GetType ();
BindingFlags flags = BindingFlags. GetProperty;
Binder binder = null;
Object [] args = null;
Object result = type. InvokeMember (propertyName, flags, binder, control, args );
Return Convert. ToString (result );
}

Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
GridView1.DataSource = new String [] {"A", "B "};
GridView1.DataBind ();
}
}
</Script>
<Head id = "Head1" runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "MengXianhuiForm" runat = "server">
<Asp: TextBox ID = "TextBox1" runat = "server" Text = "can you enter Text?"> </asp: TextBox>
<Asp: ListBox ID = "ListBox1" runat = "server">
<Asp: ListItem> AA </asp: ListItem>
<Asp: ListItem> BB </asp: ListItem>
</Asp: ListBox>
<Asp: GridView ID = "GridView1" runat = "server">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<Asp: TextBox ID = "x" runat = "server" Text = "test"/>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "set the Enabled attribute of all controls on the page"/>
</Form>
</Body>
</Html>

Author: Meng xianhui

Related Article

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.