Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net UserControl properties

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!

like image 509
LZW Avatar asked Jan 22 '12 12:01

LZW


People also ask

What is UserControl in C#?

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.

Which property can be used in Web user control to access the master page?

c# - Accessing masterpage property from a usercontrol - Stack Overflow.


1 Answers

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" />
like image 116
Joe Avatar answered Sep 18 '22 16:09

Joe