简体   繁体   English

动态设置usercontrol属性

[英]dynamically setting usercontrol property

I have a user control in asp.net that i need to dynmically set properties for. 我在asp.net中有一个用户控件,需要动态设置属性。 The propety and property values are in a dictionary, for example: 属性和属性值在字典中,例如:

Dictionary<string, string> propertyValues;
Control c = Page.LoadControl("~/Control.ascx")

I can currently set the properties on the control using reflection, however, with .net 4 and the new dynamic keyword, is there an easier way to do it, for example, something like this: 目前,我可以使用反射来设置控件的属性,但是,使用.net 4和new动态关键字,是否有更简单的方法可以做到这一点,例如,如下所示:

dynamic c = Page.LoadControl("~/control.ascx");
foreach(var itemin propertyValues)
{
  c.item.key = item.value;
}

this obviously doesnt work because item.key is a string and not a property. 这显然不起作用,因为item.key是字符串而不是属性。

thanks Raj. 谢谢拉杰。

have you tried c[item.key] ??? 您是否尝试过c[item.key] or maybe c.Properties[item.Key] ??? 或者也许c.Properties[item.Key] ???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM