1

I have a custom control in an ASP.NET Web Forms project that inherits from System.Web.UI.Control.

I want to add attributes in the markup that to not correspond to properties of that control e.g.

<myControls:Hyperlink runat=server custom-client-side-attr="1"></<myControls:Hyperlink>

The problem I am having is that the exception

Type 'myControls.Hyperlink' does not have a public property named 'custom-client-side-attr'.

I have tried PersistChildren(false), but that does not fix it. It has been a while since I have been in the depths of ASP.NET Web Forms and cannot remember how this is done.

3 Answers 3

4

You have to add those in server-code:

hl1.Attributes["custom-client-side-attr"] = "1";

You can still do it in the markup - you'd have to do it prior to the the declaration:

<% hl1.Attributes["custom-client-side-attr"] = "1"; %>
<myControls:Hyperlink ID="hl1" runat=server custom-client-side-attr="1"></<myControls:Hyperlink>
Sign up to request clarification or add additional context in comments.

3 Comments

This worked great for me in one case (adding a custom HTML attribute to a table element output by an ASP.NET RadioButtonList). The solution's kludginess is really ASP.NET's fault I think: why ASP.NET does not simply add control attributes in markup without corresponding control properties to the control's Attributes collection is beyond me; and the problem in question also generally indicates something kludgy upstream: this "nasty" gem is handy when you cannot do much about that.
In another case, no dice applying this fix in markup: System.Web.HttpException: Untrapped Exception: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Yep, I've never really understood that one - sometimes it didn't make sense why I couldn't modify a control, and sometimes didn't make sense when I could. Some of this ridiculousness is why I was so happy when aspnet MVC came out so I could stick with proper html.
0

If you want an attribute like this, you have to create a property in for the user control. You may use viewstate or hidden control to keep the property persistent. Your code may look something like this:

public string  custom_client_side_attr 
{
    get {
        if (ViewState["custom_client_side_attr"] != null)
        {
            return ViewState["custom_client_side_attr"].ToString();
        }
        return string.Empty;
    }
    set
    {
        ViewState["custom_client_side_attr"] = value;
        //set custom atribute for the hyperlink here
    }
}

And access the property through markup:

<myControls:Hyperlink id="myCustomControl" runat=server custom_client_side_attr="1"></<myControls:Hyperlink>

or in the code:

myCustomControl.custom_client_side_attr="1";

3 Comments

I cannot add a property because the Hyperlink control is generic. The attributes could be called anything
If you want to access any attribute, it must have to be exposed.
I don't want to access them server side i just want them render on the client. The same think is possible with the standard controls. I just want the attribute "pass-through" to work as the docs say it should
0

If your custom control derives from WebControl it seems to work as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.