获取页面中所有控件中某个属性

<% @ 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 Button1_Click(object sender, EventArgs e)
  {
    SetControlArrtibute(Page);
  }

  private
void SetControlArrtibute(Control ctl)
  {
    Type type
= ctl.GetType();
    PropertyInfo[] properties
= type.GetProperties();
    Response.Write(
" <hr> " );
    Response.Write(type.ToString());
    Response.Write(
" <br> " );
    foreach (PropertyInfo property
in properties)
    {
     
// 不是所有的属性都能通过这种方法进行得到?
      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 ="能输入文字吗" ></ 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 ="测试" />
       
</ ItemTemplate >
     
</ asp:TemplateField >
   
</ Columns >
 
</ asp:GridView >
 
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="设置页面中的所有控件的 Enabled 属性" />
 
</ form >
</ body >
</ html >
//转载孟宪会
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值