c# – asp.net UserControl属性
发布时间:2020-12-15 04:21:00 所属栏目:百科 来源:网络整理
导读:是否可以访问用户控件中未定义的属性?我想添加任何html属性,而不在codebehind中定义它. 例如: my:TextBox runat="server" extraproperty="extravalue" / 在用户控制中未定义的气温,但仍然产生: input type="text" extraproperty="extravalue" / 我需要在
是否可以访问用户控件中未定义的属性?我想添加任何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" /> 编辑 从评论:
以上将适用于自定义服务器控件(从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" /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |