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