shadowf
shadowf

Reputation: 1186

ASP.Net - Fire a button's event on the server-side without causing a postback

I have a question which I have seen other people have had in the past as well but I could not find any solutions to.

I want to use an ASP.Net Link Button (or even a Button) on a page which once clicked on, its event code on the serverside fires BUT I don't want it to cause a postback.

Now I know these controls are meant to cause postback by design and that you can disable this through adding: "return false;" to the Onclientclick parameter (or alternatively Attributes.Add("onclick", return false"); ) but please note that this is NOT what I am asking. I think my question essentially is: how can we call a method on the server-side from the client-side without causing a postback? Is this even possible? (I can sense some logical issues in the question itself but several other people have had the same problem).

And for you creative minds out there, I recently came across: http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

which explains how to identify the control which causes the postback. maybe this helps by somehow disabling the postback effects yet firing the event? if so, how can you avoid the page refresh effects (I don't have anything in the Page_Load, but the page still "refreshes" or re-renders. I don't want this, can this be avoided? I haven't figured out a way yet.

Thank you for your time.

Upvotes: 1

Views: 4426

Answers (5)

Junaid
Junaid

Reputation: 1755

you can do two things here. - either use update panel and add a check in PageLoad for IsPostBack. - The other thing you can do is that make a ajax call using jquery and specifying a method that would execute on that button click, following link should help you with that. http://www.seoasp.net/post/2008/07/16/jQuery-To-Call-ASPNET-Page-Methods-and-Web-Services.aspx

Upvotes: 0

Jonas B
Jonas B

Reputation: 2391

Why not just code a javascript triggered by the button that does the clientside modifications you need. If you need any dynamic data from the server during the event you can fetch that with ajax from the server.

Upvotes: 1

Dan Diplo
Dan Diplo

Reputation: 25339

To call a server side event you HAVE to send data to the server so it knows what to do. Stopping the postback stops the data being sent, hence the server will never know about what has happened on the client.

If you simply want to avoid page refereshes then the way to go is to use AJAX. In ASP.NET web-forms the simplest way is to wrap stuff in an UpdatePanel. Another way is to look into using PageMethods with client-side script (essentially calling a web-service from client-side script that then acts as a proxy to the server).

Upvotes: 3

James
James

Reputation: 7533

You could trigger a partial playback instead (so the page doesn't reload) by putting the button in an UpdatePanel.

Upvotes: 0

clearpath
clearpath

Reputation: 956

As far as I know, that is not possible.

Upvotes: 0

Related Questions