Migue
Migue

Reputation: 91

ASP.net WebForms - How get html5 data-attribute from codebehind?

im trying to get data-attribute (data-icon) from an HtmlControl ... that data-attribute is setted from a js function, but when the page do postback it returns (on codebehind) an empty string

theres any way , any property or else to get it??

My Code:

HTML:

<button id="btnIcon" runat="server" class="btn btn-default iconpicker" data-icon=""></button>

Code behind:

string icon = btnIcon.Attributes["data-icon"].ToString();

PS: the attribute is changed via jQuery by a js plugin.

Upvotes: 2

Views: 3169

Answers (1)

JP Hellemons
JP Hellemons

Reputation: 6047

@Gusman is right. You need an hiddenfield

<asp:HiddenField ID="hfDataIcon" Value="" runat="server" ClientIDMode="Static" />

and make the jquery not only set the data-icon but also the hfDataIcon value. You can also fill the hiddenfield with a startvalue when you eval bind to the value.

<asp:HiddenField ID="hfDataIcon" Value="" runat="server" ClientIDMode="Static" Value='<%# Eval("dataIcon") %>' />

and here is some jQuery to set the hiddenfield, which can be accessed in the code behind when posted back.

<script type="text/javascript" language="javascript">
$(document).ready(function () { 
    $('input#hfDataIcon').val('icon name here');
});
</script>

Upvotes: 1

Related Questions