【发布时间】:2012-10-22 20:26:03
【问题描述】:
我想基于 XML 向我的页面添加用户控件:
<?xml version="1.0" encoding="utf-8" ?>
<Fields>
<Group name="Main" text="Innhold">
<Field type="TextBox" name="Name" text="Navn"></Field>
</Group>
</Fields>
用户控件看起来像这样 TextBox.ascx:
<div class="fieldWrapper">
<asp:Label runat="server"><%=Label %></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" />
</div>
我根据 xml 中的 type 属性做了一个 LoadControl。喜欢:LoadControl(type + ".ascx"):
var fields = from x in element.Elements("Field") select new
{
type = x.Attribute("type").Value,
name = x.Attribute("name").Value,
text = x.Attribute("text").Value,
};
foreach (var field in fields)
{
var control = LoadControl("~/UserControls/FieldControls/" + field.type + ".ascx");
pnl.Controls.Add(control);
}
FieldsHolder.Controls.Add(pnl);
我想将 xml 中的文本属性传递给 TextBox.ascx 中的标签。 像这样: ctrl.Label = field.text 我知道如果我将控件转换为正确的类型,我可以做到这一点,但我不知道类型。 我可以通过某种方式使用反射吗?
【问题讨论】:
标签: c# asp.net reflection user-controls loadcontrol