简体   繁体   English

使用自定义属性实例化用户控件

[英]Instantiate User Control with Custom Attributes

My User Control has the following properties: 我的用户控件具有以下属性:

private String _requestIP;
public String RequestIP
{
     get { return _requestIP; }
     set { _requestIP = value; }
}

When adding a instance of the Control to an aspx page at design time it's easy to assign the attributes which can be utilized in the codebehind file... 在设计时将控件的实例添加到aspx页面时,很容易分配可在代码隐藏文件中使用的属性...

<uc:Item ID="Testing" runat="server" RequestIP="127.0.0.1" />

However, if I try to create the control at runtime in the aspx.cs file, how am I able to assign values to these attributes? 但是,如果尝试在运行时在aspx.cs文件中创建控件,如何为这些属性分配值?

Control ItemX = (Control)Page.LoadControl("/controls/item.ascx");

There is no ItemX.Attributes.Add() method which I would expect to be there, and no ItemX.RequestIP property to set. 我没有期望有的ItemX.Attributes.Add()方法,也没有要设置的ItemX.RequestIP属性。

Is there a way to set this dynamically in the aspx page using <%= Users_IP_Address %> tags or some other method? 有没有一种方法可以使用<%= Users_IP_Address%>标记或其他方法在aspx页面中动态设置此设置?

好吧,您只需要将其转换为适当的类型(无论用户控件的类名称是什么)。

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

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