我的页面有两个控件
TextBox1
DropDownList1我写了一个函数返回对页面对象的特定属性值object GetMyValue(string strControlServerID,string strPropertyName)
{
object obj=this.FindControl(strControlServerID);
Type t=obj.GetType();
return t.InvokeMember ( strPropertyName,BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic| BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField| BindingFlags.NonPublic
, null , obj , null );
}
调用的时候
GetMyValue("TextBox1","Text");//成功,返回TextBox1.Text的值
GetMyValue("DropDownList1","SelectedValue");//失败!提示DropDownList没有SelectedValue属性
我查了一下MSDN,发现 Text属性是 TextBox这个类的,而SelectedValue属性是 DropDownList类的父类(System.Web.UI.WebControls.ListControl)的属性,虽然我们在使用的时候用的是 DropDownList.SelectedIndex,但是通过 Type.InvokeMember却不能实现,请教各位,该如何解决这个问题呢另外,我查了在VB中可以用CallByName来实现查找指定控件的指定属性值,在C#中却没有这个方法,我在VB中写了一个类来调用这个方法
Public Shared Function CallGetPropertyByName(ByVal ObjectRef As Object, ByVal ProcName As String, ByVal ParamArray Args As Object()) As Object
Return CallByName(ObjectRef, ProcName, CallType.Get, Args)
End Function
想在C#中使用,却出现这样的提示
Service Unavailable
TextBox1
DropDownList1我写了一个函数返回对页面对象的特定属性值object GetMyValue(string strControlServerID,string strPropertyName)
{
object obj=this.FindControl(strControlServerID);
Type t=obj.GetType();
return t.InvokeMember ( strPropertyName,BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic| BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField| BindingFlags.NonPublic
, null , obj , null );
}
调用的时候
GetMyValue("TextBox1","Text");//成功,返回TextBox1.Text的值
GetMyValue("DropDownList1","SelectedValue");//失败!提示DropDownList没有SelectedValue属性
我查了一下MSDN,发现 Text属性是 TextBox这个类的,而SelectedValue属性是 DropDownList类的父类(System.Web.UI.WebControls.ListControl)的属性,虽然我们在使用的时候用的是 DropDownList.SelectedIndex,但是通过 Type.InvokeMember却不能实现,请教各位,该如何解决这个问题呢另外,我查了在VB中可以用CallByName来实现查找指定控件的指定属性值,在C#中却没有这个方法,我在VB中写了一个类来调用这个方法
Public Shared Function CallGetPropertyByName(ByVal ObjectRef As Object, ByVal ProcName As String, ByVal ParamArray Args As Object()) As Object
Return CallByName(ObjectRef, ProcName, CallType.Get, Args)
End Function
想在C#中使用,却出现这样的提示
Service Unavailable
如果只是属性的话,用下面的方法
object GetMyValue(string strControlServerID,string strPropertyName)
{
object obj=this.FindControl(strControlServerID);
Type t=obj.GetType();
return t.GetProperty ( strPropertyName ).GetValue ( obj,null);
}
{
public static void Main()
{
child c = new child();
c.ParentString = "parent";
c.ChildString = "child";
Type t = c.GetType();
object obj = t.InvokeMember("ParentString", BindingFlags.GetProperty, null, c, new object[] { });
Console.WriteLine(obj .ToString () );
}
}
class parent
{
private string parentString;
public string ParentString
{
get { return parentString; }
set { parentString = value; }
}
}
class child : parent
{
private string childString;
public string ChildString
{
get { return childString; }
set { childString = value; }
}
}
{
object obj=this.FindControl(strControlServerID);
Type t=obj.GetType();
return t.InvokeMember ( strPropertyName,BindingFlags.GetProperty,null,obj,null );
}