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 ?


see my blog at and check the future of open mobile widget solutions at
10/4/2005 3:41:53 PM 24353 articles. 1 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 11

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 single Javascript file that I include in the Header or the HTML...and rely on hidden field information to make my Scripts run correctly...

but that is just me... :-)

10/4/2005 4:41:32 PM

I do this once in a while, from Javascript you can refer to a variable like this:

var myvar = '<%#MyServerSideVar%>'

Keep in mind you'll have to call Page.DataBind() for this to be populated.

10/4/2005 4:51:48 PM


how about the other way around.

using a client side variable in ASP.NET.

in regular asp I would still be using the hidden field en do a request form after a post.

but in ASP.NET we use server side controls and I can't just fill them up with client side javascript.



see my blog at and check the future of open mobile widget solutions at
10/5/2005 7:15:19 AM
You can still use hidden fields.  Or you can also use hidden server controls, I use a hidden <asp:TextBox> when I want the values to persist between postbacks.

10/5/2005 2:26:58 PM


I can't fill server controls with client side javascript.

I get an error. and when I look at the source. I see something like   __viewstate   instead of seeing the name of the server side textbox and some (encoded?) text as the value.

The Interwanderer

see my blog at and check the future of open mobile widget solutions at
10/5/2005 2:48:14 PM
Works ok for me, what error are you getting?  The __viewstate hidden field is a built-in part of ASP.NET, you shouldn't mess with that one.  Your own controls will be farther down in the source.

10/5/2005 3:53:58 PM

Similar Artilces:

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...

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...

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.   Chen  Please "Mark as an Answer" if you think my post had helped you!Thanks. you can use ajax,javascript and cookies also.  Got what you needed. Please mark as answer. In the aspx file: <input type="hidden" id="confirmAnswer" name="confirmAnswer" runat="server"> That adds this to the...

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. ...

client-side and server-side?
hi, can someone tells me how diff is client side and server side are these two? When I tried to do validation ... I would use Javascript ... which has something like: document.forms[0] ... I think that's client side. My understanding on server side is the "form" ... like u will have <form action=".." method=post> ..... </form> ... so it will send to server ... am I on the right track? Are there any other type of server side examples? Hi, ASP.NET itself is server side. Every piece of code processed is done on a server. What comes out of this is sent ...

Server Side or Client Side
Hi I'm a bit confused about this whole server side / client side thing. Are applets, embeded objects consider client side? If I have a form within my aspx page which connects to a database on the server side what is it considered? If I have some business logic written within a VB file when the client access the functions within that business logic (for example some kind of validation function) does the client browser actually downloads that VB file? Can someone suggest how to differentiate between client side and server side components within a based website?  Hi, i can&#...

Server-side or Client-side
In general, I'm wondering if there are any criteria to use when choosing between using a client side element or a server side element. For example, if I'm going to put an HTML table on a page, how do I decide whether or not to use a server control?  Should the server side always be chosen or if there is not explicit server side functionality required is it better to opt for a "normal" client side table? Obviously, if there is a need for server side control the question is answered, I'm looking more at more ambiguous situations. Thanks  This is from Micros...

What is Client Side and Server Side?
I am New of ASP.Net What we can and should do client side and Server Side? What uses? Please any one clear my doubt. Thanking YouEndendrum Priyamudan P m R Clientside is basically what a person can see and serverside is where actions get processed. that's kinda simply puyt but thats the deal, you code serverside, and the html and javascript it generates let's the users for example click a button, then serverside you have coded what should happen when a button gets clicked for example turn text red. Then the server sends a new page with red text to the client (clientside...

Updateting Server side variable through javascript or how can i user Javascript variable in server side C# ?
 Hi I am new for web development. I wasted fewdays but I am not able to figureout following problem.1. I have Javascript variable I want to use in Server side C# is it possible ?2. If above is not possible if i declare variable in C#  and is it possible to update through Javascript ?As I am new for web development Please give me sample code for example it will be very easy for me to understand. If  both way possible please explain me both way. Thanks in anticipationHarshad   Yes it is possible to get the value of your javascript variable in C#. here's how:1. In...

Client side and server side
We have a application, where we have some validation on the clientside using EnableClientSideValidation. If you don't have fullscreen you can't see some of the error-messages. What we want is to jump to the place on the page where the message is displayed. One problem is that the button is a serverbutton (runat="server"). How can we accomplish this on the clientside using the same button. I have tried to use "onClientClick" and a link something like this:   <A HREF=>form<...

Validation Controls in .NET ( Client side or server side?)
 i have a very basic question. We have all these validation controls in ( required field, range validator and others) question is the required field validator is a server side control. So does that mean that it does a post back? it client side or server side control  The only controls that post back are buttons and other inputs where you set AutoPostback="True", such as a dropdownlist or a textbox.  Another way of posting back is using an UpdatePanel and placing a trigger in it for a specified control and/or event. All controls are considered clien...

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...

Server side Generating and client side executing javascript
Hi,  This is a really basic question.  If one has a piece of code on the server side that generates some javascript, how do you inject and execute this script n the client side?  For example, I have a server side function that inserts a bunch of calls to a the javascript function foo(), such as foo(1); foo(2);.....How can insert these calls into my webpage so that they are executed on a periodic basic.  I assume I would use an script manager tag, and inside this tag I would have an update panel, but am unsure where to go after that.  jerry   Hi, you can in...

Web resources about - server side variables in client side javascript -

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken .… ...

Resources last updated: 12/11/2015 11:27:14 PM