Click here to Skip to main content
15,850,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I've got a question for you,

I've got three buttons, i want the ID of the last clicked button in my code behind.

This is actually don't work:

JavaScript
$(function () {
    $('.btn').click(function () {
        $('.btn').attr('class', 'btn btn-default');
        $(this).attr('class', 'btn btn-primary');
        $("#<%=hfCb.ClientID%>").val($(this).attr('ID')); //hfCb is a hiddenfield
    });
});


ASP.NET
<button  runat="server" id="btnFiltreLeft" type="button" class="btn btn-default"  önserverclick="btnFiltre_Click">Réceptionnés</button>
<button  runat="server" id="btnFiltreCenter" type="button" class="btn btn-default"  önserverclick="btnFiltre_Click">Validés</button>
<button  runat="server" id="btnFiltreRight" type="button" class="btn btn-default"  önserverclick="btnFiltre_Click">Tous</button>


btnFiltre_Click must be called here. In console I can see that the hidden field has the correct value but not in my code behind.

VB
Protected Sub btnFiltre_Click(sender As Object, e As EventArgs)
    Debug.WriteLine(hfCb.Value)
End Sub


I've got a workaround, if I remove all the
HTML
onserverclick="btnFiltre_Click"


and do this in the JS

JavaScript
$(function () {
    $('.btn').click(function () {
        $('.btn').attr('class', 'btn btn-default');
        $(this).attr('class', 'btn btn-primary');
        $("#<%=hfCb.ClientID%>").val($(this).attr('ID')); //hfCb is a hiddenfield
        __doPostBack('ctl00$ContentPlaceHolder1$btnFiltre', "");
    });
});


I got the correct value in the code behind. But I want to know why I wasn't, is it due to the lyfecycle or what?
Posted

1 solution

HTML button takes one of 3 values: 'button', 'submit', or 'reset'. Default is 'submit'. In your original code, you specify type='button', it will not submit, means no post back. Either remove the type attribute or give it the value of 'submit'
<button  runat="server" id="btnFiltreLeft" class="btn btn-default"  onserverclick="btnFiltre_Click">

also, typo ö on onserverclick
 
Share this answer
 
v2
Comments
pjaar89 16-Jun-14 6:16am    
Thank you! Updating with submit has resolved the problem. And I don't know where did the bad typo come from ^^
Peter Leow 16-Jun-14 9:56am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900