2

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.

3
  • 1
    I doubt you can do it, the attribute is being changed on the client side, and to the server side only POSTed variables are sent, so attributes aren't. Maybe you can store the attribute also in a hidden field? Commented Feb 26, 2016 at 23:11
  • How are you setting the data-icon value in jQuery ? Technically, as long as the control is asp.net element, you would be able to access it's attributes. Commented Feb 26, 2016 at 23:13
  • 1
    @DinoMyte No, that's not true. Only form values are submitted upon postback. Attributes are not, unless you manually do something to put them in with the form values. Commented Feb 27, 2016 at 2:52

1 Answer 1

1

@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>
Sign up to request clarification or add additional context in comments.

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.