简体   繁体   English

如何通过标记在ASP.NET WebControl上设置自定义对象属性

[英]How to set custom object property on ASP.NET WebControl through markup

I am struggling how to set a custom property which is suppose to point to an instance of my custom class on an ASP.NET webcontrol. 我正在努力如何设置自定义属性,该属性应该指向ASP.NET Webcontrol上我的自定义类的实例。

Sample web control: 网页控件示例:

public class CustomControl : System.Web.UI.WebControls.Panel
{
  public IFactory Factory { get; set; }
}

Code behind: 后面的代码:

public partial class Main : System.Web.UI.Page
{        
  public IFactory GetFactory { 
    get { return new CustomFactory(); } 
  }
}

public class CustomFactory : IFactory {}

The custom factory get initialized on the code behind. 自定义工厂将在后面的代码上初始化。 In my markup (not in code behind), I need to set the Factory property on my CustomControl to the instance in my code behind. 在我的标记中(不在后面的代码中),我需要将我的CustomControl上的Factory属性设置为我后面的代码中的实例。 Any variation of inline code that I tried did not work: 我尝试的内联代码的任何变体均无效:

<asp:CustomControl ID="MyCustomControl" Factory="<%GetFactory%>" runat="server" />
<asp:CustomControl ID="MyCustomControl" Factory="<%=GetFactory%>" runat="server" />

Can anyone assist how to do this? 任何人都可以协助执行此操作吗?

You just can't assign it on the control tag markup, the tag markup is rendered as html and has no logic to do it that way, html won't interpret te result of GetFactory . 您只是不能在控件标记标记上分配它,标记标记被呈现为html,并且没有这样做的逻辑,html不会解释GetFactory结果。 What you can do is to set if on your markup, not it the control tag property, but inside code brackets just like this: 您可以做的是在标记上设置if,而不是control tag属性,而是在代码括号内设置,如下所示:

<%MyCustomControl.Factory = this.GetFactory;%>

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

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