Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2006, 10:59 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default .NET javascript server client communication

Is it possible to call server side functions from javascript that is running on the client?

I've done something like this, with an asp:LinkButton, I altered the href attribute to run some javascript on the client, and based on the result of the client javascript function, I choose whether to run the __doPostBack code, which I got from the compiled source code of the page (before attribute alteration)

This is not nearly ideal for my next problem - is there an easy way to call function on the server from code running on the client?

thanks,
Mark

 
Old March 21st, 2006, 01:24 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

No, a post back needs to be done to run a server side script.

Jim

 
Old March 21st, 2006, 09:34 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I needed to do was pop-up a confirm box, when the user navigated using a header, if the user clicked 'OK', I needed to save any changes (on the server) then redirect to the appropriate page.

I accomplished this by adding an asp:LinkButton for every header link in a <td style="display:none"> I wrote an onclick server event handler for each linkbutton.

Next, in PageLoad, I changed the href attribute for the header links, to run a confirm script, and if the confirm returns true, I run a postback for the appropriate hidden linkbutton, which handles the save and redirect.

Here is the line:

CType(myHeader.FindControl("HomeLink"), HyperLink).Attributes.Item("href") = "javascript:if(confirm('Would you like to save?')){__doPostBack('SavePostBackHome','');}else {window.location = 'Home.aspx'};"


Part of the problem here was that I only wanted this confirm box to pop-up a confirm on a couple pages, not for the whole project.

I've always thought it would be nice to be able to decide what server side function to call from a client side script - I think that is what I found here.

If there is an easier way to use this __doPostBack function, then this post might be a waste of time. What I found is that for some of the ASP controls, the compiler doesn't generate any __doPostBack code, which may make the HyperLink and LinkButton unique.

This same functionality may be possible by changing a variable then resubmitting the form/page and deciding what to do in PageLoad

 
Old March 21st, 2006, 10:32 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Almost every server control has an autopostback attribute (at least the ones that deal with user interaction)

"The one language all programmers understand is profanity."
 
Old March 22nd, 2006, 12:31 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

glad you found a workaround.. looks good, I may need to use your code in the future.. :)

Jim






Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript Client-Server Communication in Safari tedwardo24 Javascript 2 June 30th, 2006 07:45 AM
VC++.net USB communication dhanushkaki Visual C++ 1 March 26th, 2006 03:07 PM
ASP.NET Server Controls and Client-Side Scripts LoneStar1 BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 5 March 15th, 2006 05:51 PM
Communication between client and server components zolo Classic ASP Components 0 June 29th, 2003 12:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.