yuriy
yuriy

Reputation: 21

Add a html attribute to asp.net control (asp:HyperlinkField)

I would like to know if it's possible to add attribute to asp control like . I'm using bootstrap and I need to add this attributes (data-toogle, data-remote, data-target).

I need to add them here:

<asp:HyperLinkField DataTextField="USER_ID" HeaderText="" datanavigateurlfields="USER_ID"
                datanavigateurlformatstring="~\Delete.aspx?user_id={0}" ControlStyle-CssClass="aDelete" data-toggle="modal" data-target="#modalDialog" data-remote="false"/>

Obviously this doesn't work. Wonder if someone could help me with this.

Upvotes: 1

Views: 6147

Answers (2)

yuriy
yuriy

Reputation: 21

Solved it with JQuery, it seems that "Attributes" class isnt't defined for HyperLinkField so it is impossible to add attributes from CodeBehind or even JavaScript, I think.

$(document).ready(function () {
        $('.aDelete').click(function () {
            $('#ModalDialog').modal();
        });
    });

Upvotes: 0

DGibbs
DGibbs

Reputation: 14618

Depending on the control, you can add the attribute directly to it e.g.

<asp:HyperLink ID="foo" runat="server" Text="foo" data-foobar="hello" />

Renders as

<a id="ctl00_ctl00_plcMain_plcMain_foo" data-foobar="hello">foo</a>

Failing that you can access the controls AttributeCollection and add it there in the code behind:

foo.Attributes.Add("data-foobar", "hello");

Upvotes: 3

Related Questions