5

I have a LinkButton that I need to perform a click on to cause a postback. The actual link target is:

javascript:__doPostBack('ctl00$c1$btnRefreshGrid','');

Clicking the link does perform the postback, as verified by a breakpoint in the code-behind. Also pasting javascript:__doPostBack('ctl00$c1$btnRefreshGrid','') in the address bar of the browser works with the same effect.

I've tried the following with no effect at all:

__doPostBack('ctl00$c1$btnRefreshGrid','');    
$('#ctl00$c1$btnRefreshGrid').click();
$('#ctl00$c1$btnRefreshGrid').trigger('click');
eval($('#ctl00$c1$btnRefreshGrid').attr("href"));

I've tried using both <%= btnRefreshGrid.UniqueID %> and <%= btnRefreshGrid.ClientID %> to generate the selector.

Mark Richman
  • 28,948
  • 25
  • 99
  • 159

6 Answers6

4

You were close, this works in Firefox:

 function clickMyButton() {
   javascript:__doPostBack('<%= MYBUTTONID.UniqueID %>','')
};
Markive
  • 2,350
  • 2
  • 23
  • 26
1

the following works for the following anchor (originally asp:LinkButton in server side) inside li

<li>
<a id="ctl00_ContentPlaceHolder1_ChangeNumberItemGrd_ctl01_FindByID" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$ChangeNumberItemGrd$ctl01$FindByID','')">287573</a>
</li>

because i do not have the name i must generate it from it

$(".msglist li").on("click", function () {    
    var postbackArg = $(this).find("a").prop("id").replace(/_/g,"$");    
    __doPostBack(postbackArg, '');

});
Iman
  • 17,932
  • 6
  • 80
  • 90
0

In firebug you can get the correct name and link action of the link button:

<a id="MainContent_ctl00_Submit_Button" href="javascript:__doPostBack('ctl00$MainContent$ctl00$Submit_Button','')"></a>
Marco Castelo
  • 56
  • 1
  • 2
  • 4
0
var Eventtarget = $("#btnSave").attr("name");
__doPostBack(Eventtarget, "");
jibboo
  • 91
  • 6
0
$("#<%= btnRefreshGrid.ClientID %>").click();

Should work...

Hope it helps!!!

Carlos Mendible
  • 331
  • 1
  • 9
0

ASP.NET:

<asp:LinkButton ID="btnDelete" runat="server" CssClass="btn-u btn-u-xs btn-u-red"
OnClientClick="return get_confirm(this,event);"> <i class='fa fa-trash-o'> Delete </i> </asp:LinkButton>

JavaScript:

function get_confirm(obj, e) {
e.preventDefault()    
var postbackArg = obj.href.replace("javascript:__doPostBack('", "").replace("','')", "");    

$.confirm({
    title: 'Confirm',
    content: 'Are you sure to delete this item?',
    closeIcon: true,
    buttons: {
        confirm: {
            text: 'Ok',
            btnClass: 'btn-red',
            action: function () {
                __doPostBack(postbackArg, '');
            }
        },
        cancel: {
            text: 'Cancel',
            action: function () {
            }
        }
    }
});

}

Mohsen Najafzadeh
  • 411
  • 1
  • 6
  • 12