ACP
ACP

Reputation: 35268

span tag onclick to call a code behind method

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?

Upvotes: 2

Views: 5903

Answers (2)

Eilon
Eilon

Reputation: 25704

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().

Upvotes: 5

Related Questions