views:

520

answers:

2

Hai guys,

How to make a span tag behave exactly like an asp:button (ie) using its onclick method to call a codebehind method..

<span onclick="MyPageMethod()"></span>

I know linkbuttons can do it for me,but my question is can span tag do it for me?

+1  A: 

Call dopostback method in javascript.
See followings:
http://urenjoy.blogspot.com/2009/02/add-dynamic-buttons-and-handle-click.html
_doPostBack_Function">http://aspalliance.com/895_Understanding_the_JavaScript___doPostBack_Function
http://geekswithblogs.net/mnf/archive/2005/11/04/59081.aspx

Brij
+3  A: 

Having a span tag be clickable is generally a Very Bad Idea. It's bad for UI and it's bad for accessibility because it's not something that a user would expect. You should use anchor tags and buttons if it's clickable.

But to answer your question: Yes, it's definitely possible to do something like this! You just have to mimic what the LinkButton control does.

To get the JavaScript code that does an ASP.NET postback you need to call:

Page.ClientScript.GetPostBackEventReference()

More info is on MSDN.

Then you have to write code on the page that handles the postback and calls MyPageMethod().

Eilon
I was dynamically creating span tag and assigning it's onclick using span.onClick = which didn't work at all in ie.... changed the createElement to create an img instead and now it works.. Thanks!
JamesB