1

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?

user544079
  • 16,109
  • 42
  • 115
  • 171

2 Answers2

8

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>
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Is there a way I can do it using the hyperlink. The existing system already has a hyperlink and I don't want to change that to a Link Button – user544079 Mar 26 '13 at 23:18
0
<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

Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35