简体   繁体   中英

enable postback onclick of a hyperlink in asp.net

I have a hyperlink as

<a href="" id="link1">Click</a>

I have do enable a post back action whenever this link is clicked.

We can do

<a href="" id="link1" onclick="javascript:location.reload()">Click</a>

However, I want to do a page postback and not a page reload.

Is it possible?

Simple, use a LinkButton instead.

void LinkButton_Click(Object sender, EventArgs e) 
{
   Label1.Text="This is a postback";
}

on aspx:

  <asp:LinkButton id="LinkButton1" 
       Text="Click Me" 
       OnClick="LinkButton_Click" 
       runat="server"/>

You are using a html link, if you want to use serverside code i would suggest to use server controls. However, if you reallly insist you could call __doPostBack manually via javascript.

<a id="linkId"  href="javascript:void(0);" onclick="__doPostBack('linkId', '');">click me</a>
<a href='void()' onclick="javascript:postBackFromLink('<%=LinkButton1.ClientID %>'); return false;">Refresh Me</a><br />

http://forums.asp.net/t/1077801.aspx?client+__doPostBack+from+within+a+usercontrol+Problem+Click+event+in+code+behind+never+reached+

bu it just usefull for usercontrol based dopostbacks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM