c# – asp.net UserControl属性

前端之家收集整理的这篇文章主要介绍了c# – asp.net UserControl属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以访问用户控件中未定义的属性?我想添加任何html属性,而不在codebehind中定义它.

例如:

<my:TextBox runat="server" extraproperty="extravalue" />

用户控制中未定义的气温,但仍然产生:

<input type="text" extraproperty="extravalue" />

我需要在自定义用户控制.注意我的:在文本框之前.

TY!

解决方法

是的,这是可能的.去尝试一下!

例如,

<asp:TextBox ID="MyTextBox" runat="server" extraproperty="extravalue" />

呈现为:

<input name="...$MyTextBox" type="text" id="..._MyTextBox" extraproperty="extravalue" />

编辑

评论

asp:textBox is not a custom user control

以上将适用于自定义服务器控件(从WebControl派生),但不适用于UserControl,因为UserControl没有可以放置该属性标签:它仅呈现其内容.

因此,您需要在UserControl类中的代码自定义属性添加到其子控件之一.然后,UserControl可以将自定义属性公开为属性,如:

// Inside the UserControl
public string ExtraProperty
{
    get { return myTextBox.Attributes["extraproperty"]; }
    set { myTextBox.Attributes["extraproperty"] = value; }
}

// Consumers of the UserControl
<my:CustomUserControl ... ExtraProperty="extravalue" />
原文链接:https://www.f2er.com/csharp/91894.html

猜你在找的C#相关文章