Click here to Skip to main content
Licence 
First Posted 17 Jan 2006
Views 51,683
Bookmarked 37 times

Share a client side return value with the server side code

By | 17 Jan 2006 | Article
How to share a client side return value with the server side code.

Introduction

Most of us might have wanted to share a client side return value with the server side code. Say, for example, I want to get the user's confirmation on whether to save the existing data and move to the next page or not?

Here, I can easily display a confirmation dialog to the user through JavaScript, but how will my server side script come to know whether the user confirmed it or refused it? Can this be done? If yes, then how?

I say, yes, it is possible, but how, we will see in the sections below.

Client and Server Interaction

To pass any value to the client, we can easily do by passing the value as an argument to the client side script function.

Like, for the OnClick event of the btnShowMonths button, I've passed the action or target attribute as the argument to my ShowMonths function of the client side script.

#region Page_Load
private void Page_Load(object sender, System.EventArgs e)
{
    if(!IsPostBack)
        btnShowMonth.Attributes.Add("onclick", 
           "return ShowMonths('ShowMonths');");

Passing a value to the client side is a fairly easy task, but the issue is how to get the response back from the client to the server side code. Like, in our code, if the user confirms to show months, my code should fill the list box with the values of all the months, else it should be filled with the days starting from "01" - "31".

No doubt, AJAX is a technique we could have used, but for fairly simple tasks like this, it would be an overhead.

So to avoid the overhead, we can handle the things as described in this article.

To get the client return, we can use the __doPostBack method available in JavaScript. This method takes two arguments, first is the event target, and the second is the event arguments. Event target is the name of the method at server side that will handle the postback request fired from the client side function, and event argument is the argument return values passed from client to server side code.

After post back, these two values are automatically stored in two hidden boxes of the ASPX page, by default.

These are "__EVENTTARGET" and "__EVENTARGUMENT". These two values can be fetched using Request.Form["__EVENTTARGET"] and Request.Form["__EVENTARGUMENT"] respectively, as we have done in the code below:

#region EventArgs
private string EventArgs
{
    get{
        return Request.Form["__EVENTARGUMENT"].ToString().ToUpper();
    }
}
#endregion EventArgs

#region EventTarget
private string EventTarget
{
    get{
        return Request.Form["__EVENTTARGET"].ToString().ToUpper();
    }            
}
#endregion EventTarget

Our Handlepostback method in the code actually checks the value of the EventTarget after each post back to check which server side function or method to execute.

#region HandlePostBack
void HandlePostBack()
{
    switch(EventTarget)
    {
        case "SHOWMONTHS":
            ShowMonths();
            break;
    }
}
#endregion HandlePostBack

If our client side code posts back some other target methods, we can add them to our list here in the HandlePostBack() method.

[Note: this will only work if at least one control on our web page has the AutoPostBack property set to true, else it will throw a JavaScript runtime error for __doPostBack not found.]

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

NeeruVerma

Web Developer

India India

Member

Software Developer with 5+ Years of experience.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberbairamsunil2:22 10 Jan '12  
GeneralGud example PinmemberAmit kumar pathak21:15 28 May '09  
Generalproblem with this Pinmemberrachel__6:54 28 May '07  
Hi,
 
I did the same as you described, but the server side method didn't fired.
 
The java script in the client side is:
 
function OnClientItemClicked(sender, args)
{
var result= window.prompt("Enter...","");
__doPostBack("HandlePostBack",result);

}
 
The code in the server side is:
 
protected void HandlePostBack()
{
TextBox1.Text = "2";
Console.WriteLine("DD");

}

I've run this in FireFox and saw that the "__doPostBack" is called. However, the code in the server side isn't called.
 
Any help will be appreciated.
Rachel__
General__doPostBack PinmemberDrieske9:50 18 Jan '06  
General2 times a postback-event PinmemberDrieske21:01 17 Jan '06  
AnswerRe: 2 times a postback-event PinmemberNeeruVerma22:47 17 Jan '06  
GeneralRe: 2 times a postback-event Pinmemberbillxie6:31 18 Jan '06  
GeneralRe: 2 times a postback-event [modified] PinmemberTins306:22 10 Oct '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 17 Jan 2006
Article Copyright 2006 by NeeruVerma
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid