Is it possible to access properties not defined in user control? I want to add any html attribute without defining it in codebehind.
ex:
<my:TextBox runat="server" extraproperty="extravalue" />
where extraporperty not defined in user control, but still generates:
<input type="text" extraproperty="extravalue" />
I need this in custom user control. Notice the my: before the textbox.
ty!
Definition of C# User Control. C# user control is defined as an implementation in programming language of C# to provide an empty control and this control can be leveraged to create other controls. This implementation provides additional flexibility to re-use controls in a large-scale web project.
c# - Accessing masterpage property from a usercontrol - Stack Overflow.
Yes it's possible. Just try it!
For example,
<asp:TextBox ID="MyTextBox" runat="server" extraproperty="extravalue" />
renders as:
<input name="...$MyTextBox" type="text" id="..._MyTextBox" extraproperty="extravalue" />
Edit
From comments:
asp:textbox is not a custom user control
The above will work for a custom server control (derived from WebControl), but not for a UserControl, because a UserControl does not have a tag on which the attribute can be placed: it only renders its contents.
So you would need code in your UserControl class to add your custom attribute to one of its child controls. The UserControl could then expose the custom attribute as a property, something like:
// Inside the UserControl
public string ExtraProperty
{
get { return myTextBox.Attributes["extraproperty"]; }
set { myTextBox.Attributes["extraproperty"] = value; }
}
// Consumers of the UserControl
<my:CustomUserControl ... ExtraProperty="extravalue" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With