LZW
LZW

Reputation: 1045

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!

Upvotes: 12

Views: 14650

Answers (4)

Andacious
Andacious

Reputation: 1172

Yes, take a look at the IAttributeAccessor interface. the ASP.NET UserControl object explicitly implements this interface. This allows any attributes added directly to the control in the markup to be transferred to the server side attribute collection.

Note that the default implementation on UserControl is not overridable but writes and reads from its internal attributes collection. To render these attributes to HTML in your user control, do something like this in the markup:

<div runat="server" ID="pnlOutermostDiv">
// control markup goes here
</div>

then in the code-behind of the user control do something like this:

protected override void OnPreRender(EventArgs e)
{
    foreach (string key in Attributes.Keys)
    {
        pnlOutermostDiv.Attributes.Add(key, Attributes[key]);
    }

    base.OnPreRender(e);
}

Now when you do use the control like this:

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

It will render like this:

<div id="ctl00_blablabla_blablabla" extraproperty="extravalue">
// rendered control HTML here
</div>

Upvotes: 1

Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

You actually don't have to declare properties to use them as attributes. Take this very simple example:

<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="uc" TagName="Test" Src="~/UserControls/Test.ascx" %>

<uc:Test runat="server" extraproperty="extravalue" />

Inside your user control's code file you can get the value from any property like this:

protected void Page_Load(object sender, EventArgs e)
{
  string p = Attributes["extraproperty"];
}

As you can see, all attributes that are put on your user control can be read through the Attributes collection using the name of the attributes as the key to get the value from the collection.

Upvotes: 9

to StackOverflow
to StackOverflow

Reputation: 124686

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" />

Upvotes: 9

Jason
Jason

Reputation: 15931

you should be able to add attributes to the controls Attributes collection.

Upvotes: 2

Related Questions