Call server side function from client side javascript

I have a javascript where I either need to call an onclick even of a server link button or I need to be able to call a server side function.   The only thing is I cant use AJAX.  Is there any way I can do this or am I spinning my wheels.

Thanks!


-------------
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
0
spafa9
8/15/2007 6:42:27 PM
📁 asp.net.client-side
📃 24353 articles.
⭐ 2 followers.

💬 10 Replies
👁️‍🗨️ 3893 Views

I have a similar problem.  In my case, I would like to set a server-side session object from JavaScript.  Any suggestions?

 Thanks.

 Dan

0
dhurwitz
8/15/2007 7:44:11 PM

Sure, you can do that.  If you just want to call a click event of a button, you can do it like this:

    var btn = document.getElementById('LinkButton1');
if(btn == null)
alert('Button not found!');
else
btn.click();


And if you want to call a server-side function, just setup an invisible button:

    <asp:Button ID="Button1" runat="server" style="display:none;" />


And in the code-behind for that button, call your server-side function (and then use the javascript snippet above to invoke this event handler):

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Call your function
    End Sub
 
Hope this helps!  Please mark the helpful post(s) as Answer.
Josh Stodola † Come check out my blog!
0
JoshStodola
8/15/2007 7:44:17 PM

Spafa and Dan-

Josh has posted a very unique way to address this situation. One challenge, though, is that this proposed method will still require your page to do a full PostBack. In essence, your simply using JavaScript to simulate an user clicking on a button. If that works for you, definitely use his code.

On the other hand, it sounds like you might be after a JavaScript solution that does not require a full page PostBack. You want to asynchronously call server methods and set server variables from client script, right? If that's the case, Ajax really is your best bet. The XmlHttpRequest that makes up the foundation of Ajax is by far the easiest and most cross-browser compatible method of asynchronously interacting with the server from JavaScript.

Can you be more specific on why Ajax is out of the question for your application? Perhaps I can help you work around the issues that are blocking you from using it in your site.

Thanks~
Todd
 


Todd Anglin
http://telerikwatch.com
0
tanglin05
8/15/2007 8:30:06 PM

This worked great for my situation.

Thanks.

0
dhurwitz
8/15/2007 8:31:17 PM

You are correct that it causes a postback.  However, since I can use Ajax, I just wrapped the button in an UpdatePanel and all is hunky-dory.

0
dhurwitz
8/15/2007 8:41:13 PM

Dhurwitz-

If you can use Ajax, then you have a few more options that enable you to call page method or Web Service methods without using the invisible button hack. For information on both of the available approaches using ASP.NET AJAX, check out these resources:

http://ajax.asp.net/docs/mref/P_System_Web_UI_ScriptManager_EnablePageMethods.aspx

http://www.asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

You may not need these more "elegant" approaches, but I wanted to make sure you were at least aware.

Thanks~
Todd
 


Todd Anglin
http://telerikwatch.com
0
tanglin05
8/16/2007 1:57:38 AM

Hi Todd,

I agree that in most cases AJAX would suffice this scenario better.  And I probably would have suggested that if the OP had not declared "I can not use AJAX" in his original post.

Anyways, I am glad that my proposed solution has worked for some.

Please mark the helpful post(s) as Answer.


Josh Stodola † Come check out my blog!
0
JoshStodola
8/16/2007 1:57:28 PM

Josh-

I understand. I saw the same "restriction" in the original post and understood completely where you were coming from. Unfortunately, the thread split into two approaches: one that could use Ajax and one that could not. I think we have the solution for the "can use Ajax" approach (he's using your approach in an UpdatePanel), but I'm not sure we satisfied the original poster. :)

Either way, like you I am glad we could provide unique approaches to the same problems.

Thanks~
Todd


Todd Anglin
http://telerikwatch.com
0
tanglin05
8/16/2007 4:05:24 PM

Thank you for your post last night I needed up finding a work around to my issue.  This is what I did incase someone else finds themselves with the same issue...

 On my masterpage  I have this javascript...

<SCRIPT language=javascript>

function confirmSave(control, btnSave, btnCancel, validationGroup)

{

if(control.contains(document.activeElement)== false)

{

if(confirm('You are attempting to continue without saving. Click OK to save. If you click Cancel you WILL LOSE any changes you have made.')== true)

{

WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(btnSave, '', true, validationGroup, '', false, true));

}

else

{

WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(btnCancel, '', false, '', '', false, true));

}

}

}

SCRIPT>

 

 on my sub master page I have this javascaript this is the name of my content place holder on the sub master page.

 

<SCRIPT language="javascript">

var contentpage='ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolderSubMasterInfo$'

 

SCRIPT>

 

 

Now in my formview EditItemTemplate I have a table with all my controls I naed the table in the hmtl and added the ondeactivate like this....

 

<table id="tblEdit" cellpadding="3" cellspacing="0" height="100%" width="100%" ondeactivate="confirmSave(this, contentpage + 'fvContactInfo$UpdateButton', contentpage + 'fvContactInfo$UpdateCancelButton', '')" >

 

I have some controls that post back and have multiple formviews so I set up a session variable when it goes into editmode I set that sessionvariable so on the post back I know which formview to set focus back to in the page load  after the if not ispostback.

Thanks for all of you help

Spafa9


-------------
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
0
spafa9
8/16/2007 4:17:42 PM

Hi Josh,

Thanks a lot, really great work..

I spent almost 3 hours on it, finally your solution worked for me.

Thanks Again !!! 

 


Sachin S Dhake
Do what you LOVE, Love what you DO
0
sachin
1/3/2008 3:23:22 PM