(论坛答疑点滴)如何对动态的对象进行操作,属性付值

问题的具体解释:
数据表1纪录控件的名称,
数据表2纪录控件的属性和方法
如何实现对控件的属性进行付值,方法进行调用
我的意思是:
表1   字段     OCXid     OCXName
                 1       textbox
                 2       combobox

表2   字段     OCXid     ProId     ProValue
                 1          1       text
                 1          2       top
                 2          1       text
                 2          2       top
等等

我要实现的是:
     有一个textbox1(动态产生的)
    
       dim tmpstr as string
       tmpstr=ds.tables("表2").rows(0).items("ProValue")
       textbox1.tmpstr="abcd"  '实现的目的是这样,但不能这样写,不知该怎样


答:
TextBox t=(TextBox)this.FindControl("TextBox1");
PropertyInfo info = (t.GetType().GetProperty("Text"));
if(info != null)
{
object val = info.GetValue(t,null);
if(val != null)
this.Label1.Text=val.ToString();
}

注意别忘记
using System.Reflection;
其中
"TextBox1"换做你数据库里面读出的控件名字
"Text"换做你数据里面读出的控件的属性

原文地址:https://www.cnblogs.com/lovecherry/p/125523.html