Passing variable between client side and server side

For example,how do I pass the return value of the confirm("whatever") to the server side? Or the opposite way. 

I want to use a cookie to do is. Can some one give me hint.

 Much appreciate your help.




Please "Mark as an Answer" if you think my post had helped you!

8/10/2007 2:23:55 PM 24353 articles. 2 followers. Follow

13 Replies

Similar Articles

[PageSpeed] 53
Get it on Google Play
Get it on Apple App Store

you can use ajax,javascript and cookies also.


Got what you needed. Please mark as answer.
8/10/2007 4:27:13 PM

In the aspx file:
 <input type="hidden" id="confirmAnswer" name="confirmAnswer" runat="server">

That adds this to the aspx.cs file:
 protected System.Web.UI.HtmlControls.HtmlInputHidden confirmAnswer;

Then add to the Page_Load event handler:

string scriptString = "<script language=JavaScript>\n";

// Store the confirm's return in the hidden control...
scriptString += "document.getElementById('" + confirmAnswer.ClientID + "').value = confirm('Overwrite?');\n";

// Do a new PostBack...
scriptString += GetPostBackEventReference(confirmAnswer, string.Empty) + ";\n";

scriptString += "</script>\n";

RegisterStartupScript("ConfirmScript", scriptString);

if ( IsPostBack && Request["__EVENTTARGET"] == confirmAnswer.ClientID )
     // Response.Write("confirmAnswer.Value: " + confirmAnswer.Value);

     if ( confirmAnswer.Value == "true" )
          // User answered OK
          // User answered Cancel

confirmAnswer.Value = string.Empty;


8/10/2007 4:31:30 PM

you can use ajax,javascript and cookies also.

Wow, very helpful!! Hmm


Why would you need the answer to confirm on server-side?  If you provide us some details about what you are trying to do, then we can provide the best answer possible.  Thanks...

Josh Stodola ← Come check out my blog!
8/10/2007 4:42:04 PM

I have to do a confirm() right before the page is loaded( I will ask if the user wants to load some old settings).

I used javascript over api messagebox because the api message do not pop up on top of the current page sometime. (This page that calls confirm is redirect by a link button which is inside a materpage. If the master page do not have focus, messagebox show up on the taskbar.)


Is their a better way to do this?

Please "Mark as an Answer" if you think my post had helped you!

8/10/2007 8:03:12 PM

Set up an ASP.NET button like this, and in the code-behind for this button load your old settings, etc:

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

And this in your Page_Load:

    Dim strScript String = "if(confirm('Reload old settings?')) "
    strScript += "document.getElementById('" & btnOldSettings.ClientID & "').click();"
    ClientScript.RegisterStartupScript(Me.GetType(), "confirmScript", strScript, True)
Hope this helps!  Please mark the helpful post(s) as Answer.
Josh Stodola ← Come check out my blog!
8/10/2007 8:12:37 PM

This is a good idea, but when I try to use it, it's telling me document.getitembyId is null or not a object??????

Please "Mark as an Answer" if you think my post had helped you!

8/10/2007 8:40:41 PM

Make sure you are using the correct ID for the button.  I used "btnOldSettings" in my example.

Josh Stodola ← Come check out my blog!
8/13/2007 1:54:43 PM


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

And this in your Page_Load:

    Dim strScript String = "if(confirm('Reload old settings?')) "
    strScript += "document.getElementById('" & btnOldSettings.ClientID & "').click();"
    ClientScript.RegisterStartupScript(Me.GetType(), "confirmScript", strScript, True)


I used the same code but in c#. What does the &s do in ('" & btnOldSettings.ClientID & "')?

Here is what I put in page load:(I used your code in aspx file)

string str = "<script language=JavaScript>";

str += "var ret = confirm(\"Do you want to load old filter?\");";

str += "if (ret == true)";

str += "{";

str += "document.getElementById(btnOldSettings.ClientID).click();";

str += "} ";

str += "</" + "script>";

ClientScript.RegisterStartupScript(Page.GetType(), "confirmScript", str);


Please "Mark as an Answer" if you think my post had helped you!

8/13/2007 3:16:46 PM

There's your problem.  "&" is used as a string concatenation in VB.  Fix:

    str += "document.getElementById('" + btnOldSettings.ClientID + "').click();"

Don't forget to mark the helpful post(s) as Answer.

Josh Stodola ← Come check out my blog!
8/13/2007 3:29:15 PM

I made the correction, but it's still telling me that the button is undefined. Does it matter where I am puting the button?(I put it just after the form tag).

I used a linked button to forward to this page.

 Thanks for your help

Please "Mark as an Answer" if you think my post had helped you!

8/13/2007 3:53:15 PM

NO WAY!  You must be missing the apostrophes in my sample above.  Copy and paste it.  There is no way you can still get the same error!

Josh Stodola ← Come check out my blog!
8/13/2007 3:59:40 PM

Ya, you are right.

Thank you very much.

Please "Mark as an Answer" if you think my post had helped you!

8/13/2007 5:03:27 PM

Sure, glad you got your problem solved!  Please mark the helpful replies as Answer for the benefit of future readers.  Thanks!!

Josh Stodola ← Come check out my blog!
8/13/2007 5:05:44 PM

Similar Artilces:

How to pass a server side string variable to client side
Hi, In my application I want to send(or pass) a server side string variable to the client so that the client side javascript can operate on that string. I am new to ASP.NET. Can anyone please help me with some code snippets? If you allow a postback for this passing, you could store the string in a HiddenField and use javascript to read its value after a post~ If you want it to be more smooth and you are using 2.0, Script Callback is a good choice, there is a sample at the buttom of this page. Furthermore, some ajax framework will help you to call page method from client esa...

passing client side variable value to server side
 i have a grid and  by click on the rows, the onrowclick event is fired on client side. I want to pass the row index to the server side. As there are not sessions variables in javascript so i am stuck to pass the row to the server side.. I have made a texbox and set its valueto the index in the onrowclick event but when refresh calls, the text box became empty as the value set is on client side, i want page to persistthe value,  How can i do that? Tel me some way passing the row index to the server side?? Hi, You can call GridView_SelectedIndexChanging event on row click ev...

Client side code to pass values to server side variable?
Hi there, I have ListBox_SelectedTextChanged() method that creates another list box in the runtime as follows: // Get Action Level List lbxActionLevel = new ListBox();lbxActionLevel.ID = "ddlA" + i.ToString(); lbxActionLevel.Width = 100; lbxActionLevel.Rows = 2;SqlDataReader dr = new BusinessLogicLayer().bllGetActionLevels(); lbxActionLevel.Items.Clear();while (dr.Read()) {ListItem lAction = new ListItem(); lAction.Text = dr.GetString(1).Trim();lAction.Value = Convert.ToString(dr.GetInt32(0)); lbxActionLevel.Items.Add(lAction); lbxActionLevel.SelectedIndex = 0; } I ...

Newbie Dev: Passing/Saving variables client side to access server side.
Hello, As you might have guessed by the title of the thread I am really new to this, this is my first proper development project and my first go at ASP.NET... so as you can imagine I could do with some help! :S As the title suggests I am having trouble thinking of a way to access variables, when it comes to seperate client and server side processing. I am accessing my database through a webservice so dont want to re-fresh every time the user performs an action so I have a grid that I am adding and subtracting from on the client side using JScript what I would like to do is stor...

Firing both Client side event and server side event for server side button
I am having some difficulties getting this to work.. I have button_click ( and ClientClick (Javascript).  Javascript is firing fine.  Server script is not. may be you have return return false in the in the javascript which is why its not firing server side event.You should add javascript evelt like thisbutton1.onclientclick = "functionname" Function should nopt return false. Vikram www.vikramlakhotia.comPlease mark the answer if it helped you...

how to transfer client-side variable to server-side
hi all, i need just a small hint on how to go on... i am adding attributes to a rows in a gridview when data is bound e.Row.Attributes.Add("onClick", "MySelectedRow.text = '" + e.Row.DataItemIndex.ToString() + "';location.href='admin_contact.aspx'") this allows me to store the selected row in a hidden field (MySelectedRow). after the page is being resubmitted it resets the value of the hidden field to null. i need to have the value on the server-side. i prefer not to do the querystring unless you know how to clear the querystring i...

How to pass values from client side to server side
Hi, Is there a way to transfer variable from a client loop to a server loop. For example: var clitest = 'test'; Now I would like to get test variable from javascript into a server string variable <% sertest = ?? %> I understand that this can be done by storing a variable into a hidden control for example and than access it using server side, but when I have to transfer an array for example it get's a little more complicated. I was wondering is there more efficient approach? You can't transfer arrays, only strings. To workaround this problem pass delimited stri...

server side variables in client side javascript
I want to use an variable in client side javascript.In regular asp I used hidden fields.what is the best way to do this in ASP.NET ?thx,Intersee my blog at and check the future of open mobile widget solutions at By and large, I personally still use Hidden fields to store information, that either my Javascript function or my code-behind use.  I barely use the RegisterScriptBlock method(s) and that is only for situations where I want to be really dynamic with the client side functionality.  95% of the time, I can still use a...

Passing Client side variables from Java script to sever side variables
HiI have some code that worked fine in Net 1.1 but now doe's not work in Net 2.0 what I want to do is pass a couple of variables generated in javascipt on the client side to the server to use in server variables, below is the code that used to work var imageheight,imagewidth;imageheight = (document.images[0].height) + 80;imagewidth = (document.images[0].width) + 50; <!-- WindowWidth and WindowHeight are hidden Input feilds document.Form1.WindowWidth.value = imagewidth;document.Form1.WindowHeight.value = imageheight;Up to the imagewidth and the imageheight variables it works in so ...

How to pass the client side values to server side?
 hello to All,Actually i wanna implement a code like... a string in HTML textbox retrieve in server side control....suppose i hv a HTML control <input type="text" id="txtHTML"/>, and dis control  txtHTML.text="abc".now i want to retrieve dis client side string "abc" in d server side textbox named "txtASP"...which hs a property runat="server"so please give me an idea how to pass values from client side to server side.... hope u understand a problem... sam.  You could do this <input type="text"...

How to pass server side array to client side
Hello Friends.....   i want an intelligent textbox.....which pop up a list of data when any character is pressed in perticular textbox.......... the data should be related fields which are stored in database .........   for that i am having a javascript...........but it is static has a static array and generates output..... but i want that particular with my own datafields.......   so please any one help me...... i will be very thank full........   Thanx and regards sandeep prajapatiBhaaratRemember to click "Mark as Answer" on the post t...

Getting my client-side variable over to server-side
Ok so I have looked at a few posts on this subject and I can't quite figure out how to apply the idea to what I am doing here. I am using javascript to take amounts from 7 different textboxes and add them up on the fly, dropping the total in the 8th textbox. After they fill in that information they click button (postback) to continue throughout the form... what I need is for the "textboxTotalAmt" to move from client-side to server-side.  So I can access that total amount later in the form. Here is the Javascript piece... <script type="text/java...

Server side Variable in client side script
Hello, I want to access the variable defined in the server side script from the client side. Can anyone help out with an example ??? TIA, Rakesh Babaria I think what you do is write is as a java script variable, so something like this: <!--SCRIPT var ls_serverVar ls_serverVar = "whatever"; document.writeln("<SCRIPT language=JavaScript>"); document.writeln("var xyz = " + ls_serverVar); document.writeln("</SCRIPT>"); --> Then in your client script, you'll be able to get XYZ. ...

How to Access/Pass Server Side Array Variable at Another Web Form's Client Side
Hi All,I struck up with some intrested senario at my application development...hope some body will guide me or sends me some code snippets....Thanks in advance.Senario is like A.aspx (using C# .net 2.0 as code behind) I am going to have a array variable basically it is an TWO Dimensional array after populating the values in array I need to redirect/ reload another web form B.aspx, here i need to access the array in B.aspx client side JavaScript.How can I do this...Thanks AgainRaju Dont do a redirect but use server.transfer. That way the previous page will still hold its value, ...

Web resources about - Passing variable between client side and server side -

Variable - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Westpac interest rate rise is one of many variable loan rate rises
WESTPAC is not alone in hitting its owner-occupier customers with a surprise interest rate rise, as dozens of other variable rate home loans ...

Airbnb upgrades variable pricing tool for hosts
Online home rental group Airbnb is rolling out an upgraded variable pricing tool that automatically raises or cuts room rates based on supply ...

Commonwealth Bank raises variable home loan rates
Home owners' mortgage costs are set to rise by hundreds of dollars a year, after the Commonwealth Bank became the latest bank to hike interest ...

CBA lifts variable home loan rates to offset costs associated with stricter capital requirements
The Commonwealth Bank to lift its home loan interest rates independently of the Reserve Bank to partially offset costs associated with stricter ...

Washington Square Signs Spielbergs, Choi Joins Variable and More
... films and an episode of the HBO comedy series "Girls," on which he plays the role of Ray Ploshansky. New York-based creative collective Variable ...

YouTube v10.32 Tweaks Some Interface Elements And Includes Hints About Virtual Reality Content And Variable ...
Forget Update Wednesday! Monday is the new big day. Not only were we treated to the latest M preview release and the official Marshmallow name, ...

"You have to ignore many variables to think women are paid less than men. California is happy to try." ...
Writes Sarah Ketterer in The Wall Street Journal in "The ‘Wage Gap’ Myth That Won’t Die" (which you can get to without subscribing if you Google ...

Staley: 'A lot of different variables...been a hard year'
... 41° Navigation Home Giants A's Sharks Warriors Kings 49ers Raiders Quakes Insiders More Tickets Shop Watch Staley: 'A lot of different variables...been ...

Expert Reveals the Latest Variable Capacity Technology
Learn about gForce Ultra CRAC equipment, the latest variable capacity technology to lower energy usage and increase reliability.

Resources last updated: 1/16/2016 5:07:09 PM