Leonard Wong leothelion96 Leonard Wong leothelion96 - 6 months ago 85
jQuery Question

Trigger a click on LinkButton

I am trying to trigger a click on LinkButton Home when #newHome is clicked
I have the trigger function below but it does not seem to be working

<li id="newHome"><a href="#" style="font-size: 1.05em;">Home</a></li>

<asp:LinkButton ID="LinkButton_Home" class="menuLink" runat="server" OnClick="LinkButton_Home_Click"><div class="menuIcon">Home</div></asp:LinkButton>

$(document).ready(function () {
$('#newHome').click(function (e) {
$('#' + '<%=LinkButton_Home.ClientID%>').trigger('click');
});
});

Answer

Use Page.ClientScript.GetPostBackEventReference()

In order to simulate the postback event for asp.net control you could use Page.ClientScript.GetPostBackEventReference() to raise that event directly.

<li id="newHome"><a href="#" style="font-size: 1.05em;">Home</a></li>

<asp:LinkButton ID="LinkButton_Home" class="menuLink" runat="server" OnClick="LinkButton_Home_Click"><div class="menuIcon">Home</div></asp:LinkButton>

$(document).ready(function () {
        $('#newHome').click(function (e) {
            <%=Page.ClientScript.GetPostBackEventReference(LinkButton_Home, String.Empty)%>                
        });
    });