Client side code accessing server side variables

I am attempting to send a variable value from C# to the page for Javascript to access and use.  Basically I am using a prompt window in my server side javascript to capture a value and update the database with it.  If the value entered is not unique (because this is a unique identifier) I return a value indicating this.  I want this value available to my javascript code so I can provide an alert message telling the user to try again with a unique value. 

If there is a way to make the message come right from my C# code behind page that will work for me too.

Thanks in advance,

Mark F.

6/20/2008 5:55:13 PM 24353 articles. 2 followers. Follow

11 Replies

Similar Articles

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

You need to use Webmethod for this. Check this link


Make sure you click "Mark as Answer" for any post which has helped you.

My Blog

"Don't be afraid to be wrong; otherwise you'll never be right."
6/20/2008 6:06:50 PM

string myMessage = "something";

ClientScript.RegisterClientScriptBlock(typeof(Page), "anyuniqueid" + DateTime.Now, "alert('" + myMessage + "');", true);

All that glitters is gold-
Only shooting stars break the mold.
6/20/2008 10:00:56 PM

Hi Mark

You can store your result in a server side property (IsUnique for example) and then pick it up on the client side thus:

<script type="text/javascript">

   var isUnique = <%= IsUnique %>


    // alert user.


Javascript is case sensitive so if your IsUnique server side property has a bool return type (and if I remember rightly), you might need to do <%= IsUnique.ToString().ToLower() %>




Chris Williams MCP
Software Consultant
SantiAms Limited
6/20/2008 10:46:41 PM

ScriptManager.RegisterStartupScript(Page, Page.GetType, "ShowAlert", "alert('" + UniqueNo + "');", True) 

Maulik Patel
MCTS, Software Engineer

Don't forget to click "Mark as Answer" on the post that helped you. This will give you point and help readers to know which post solved your issue and make their search easy.
6/21/2008 7:30:06 AM

I am agree with the suggestion of Monket and recommend it because if u use ScriptManager.RegisterStartupScript method to add javascript variable in the form, the variable will only be accessible though the script which is defined inside form tag.

So, declare one public variable and use it in aspx page like <%=strVal%>.


Please mark as Answer if it helps u. Thanks!

Parth Patel
Techsture Technologies
Software Developer
6/21/2008 8:26:11 AM

Hi Monket,

Thanks for your response -- the value I want to check is located in the C# code behind page in the page load section.  So my code looks like this:

int result = classobject.somefunction(par_1, par_2);

I attempted to use your example in the javascript section of my page and it looks like this:

var code_check = <%= result.ToString().ToLower()%>;

alert("The code you entered is already in the system please try again! " + code_check;

I also tried this:

var code_check = <%= result %>;

alert("The code you entered is already in the system please try again! " + code_check;

Unfortunately I get an error when I attempt to run saying:

Error 336 The name 'result' does not exist in the current context C:\location\mypage.aspx 2325 


6/23/2008 1:17:58 PM


It should work ok so long as the result variable is declared either public or protected and that it is a member variable of the webforms code behind class.

Let me know if it's still not working.


Chris Williams MCP
Software Consultant
SantiAms Limited
6/23/2008 5:34:27 PM

So on my next attempt in my C# code I have:

public string result_function()


return 'T';


And in my JavaScript I have:

var result = <%= result_function%>;


The error generated now is:

Error 337 Argument '1': cannot convert from 'method group' to 'char' 

Your thoughts...

6/23/2008 6:04:07 PM

Hi Chris,

One other thing how do I check or ensure that the variable is a member variable of the webforms code behind class?


6/23/2008 6:10:24 PM


 You'll need to put return "T" rather than return 'T' (double quotes).  And then your second line should read

var result = <%= result_function() %>

(note the parentheses).  That should do it, I think.  No need to worry about the member variable bit since your result is being returned from a public method.

Let me know if you still have problems and I'll put a demo together.



Chris Williams MCP
Software Consultant
SantiAms Limited
6/23/2008 10:20:00 PM

Thanks for your help Chris -- I have decided to handle this with an admin page -- appreciate your patience.


6/24/2008 6:53:39 PM

Similar Artilces:

Accessing Client side variable from server side code?
Hi,  I am trying to use vbscript to run a process on the client machine. Once that process finishes I want to use the resulting value in my server side code behind file. Does anyone know a way to do this? I know that I can set server side session objects to a client variable by doing something like this: <script language="vbscript" type="text/vbscript">  strSubject = <%=Session.Item("Subject") %> </script>  but I have no idea how to do that in reverse. Thanks!    Filed under: too much coffee.  LOL! ...

Accessing Client side variable on Server side (PostBack)
New to ASP.NET. Can anyone tell me how to access client side variable on server side ie. while postback thanks, The best way to access arbitrary clientside values from the serverside is to store the values in a hidden input instead of a variable. How to access the client side variable from the server side variable in case of the htc file. You can use hidden variables. Here's an excellent post that will guide you in the right direction : Hi...The Client side variable's value can be&nb...

Accessing server-side variables from client-side events
This is a multi-part message in MIME format. ------=_NextPart_000_00E8_01C04D70.7EAC7080 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable In a client-side itemchanged function, how can I set a server-side = variable?? I simply need to execute the following: psSession.SetValue("userID", user_id); I'm sure there's a way to trigger server-side script and pass in = variables. Any suggestions?? Thanks. ------=_NextPart_000_00E8_01C04D70.7EAC7080 Content-Type: text/html; charset="iso-8859-1" ...

Loading client side variables from server side code
Just curious whether anyone has seen a better way to implement the following. It doesn't seem like the best way! What I'm doing is passing in an array of values to the client via server side code. The reason for this is because I'd like the server to do most of the crunch work and then pass the final results to the client. I use the 'onload' event of an image. code behind: imgLoader.Attributes.Add("onload", "Init('Tag1;Tag2;Tag3')") client-side. The page also contains an image called 'imgLoader'. var Entities = new Array(); function Init(s) ...

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

client side code & server side code
what is the difference between client side coding & server side coding ? when to use client side & server side code? Client side code runs on the user's browser and includes languages such as javascript and vbscript. Server side code runs on the web server and can be written in languages such as ASP.NET, PHP, ASP, ColdFusion etc. Website Design Darlington - - Experts, Information, Ideas & Knowledge - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer...

client side and server side code
Making things clear:When creating a web application one must know the difference between client side code and server side code.client side code is the code that is executed on the computer of the user. server side code is the code that is executed on the ....server.why is there client side code and server side code?well... server side code exist for the reason of sharing data with multiple users. for example: accessing a database.ASP.NET is a server side technology. in you can use different languages such as VB.NET or C#.client side code is used to perform tasks on the computer of th...

Is it possible to call client side function from server side code without initial client request?
Atlas is great. I'm using timer to periodically check database changes on server to populate client side controls with new data. Is it possible to do it without a lot of client requests to server which takes a huge useless network traffic? Server should call client side function on all connected clients when, for example, 'database updated' event occurs on server. Thanks. Hi~ Just for your interest, I think you could have a look at Comet, many web IM like Gmail's embeded Gtalk and are built on this architecture你好! Just FYI o_O...

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

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

Client side to execute server side code
I am maintaining an ASP.NET C# application written by someone else.  I am essentially a newbie to web development especially client side stuff.  Based on reading these forums I understand that I can't directly call a server side method from the client side and I have to use post pack in some way (using a webservice is not an option here).  My confusion comes from the HTML setup being used.The page has three frames; a header, body and footer.  Each frame is filled by it's own ASPX page.  The footer contains buttons for navigating to another page and some of the b...

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

Web resources about - Client side code accessing server side variables -

Highest Percentage Of Opera Mini Users Accessing Facebook? Macau
If you were asked to guess which country had the highest percentage of users of Opera mobile Web browser Opera Mini users accessing Facebook ...

International Users Accessing Facebook Places Through US VPN Accounts
By using a virtual private network (VPN) hosted in the United States, Facebook users from around the world are accessing Facebook Places. The ...

What are some alternatives to Yodlee for accessing bank information?
Clay Loveless , Founder, Jexy. Co-founder, Mashery. Founder, Jexy. Co-founder, Mashery.

Cloud Console - Accessing files in cloud storage for iPad on the iTunes App Store
Get Cloud Console - Accessing files in cloud storage on the App Store. See screenshots and ratings, and read customer reviews.

Meryl Streep On Accessing The Characters Within - YouTube
Meryl Streep talks about the importance of an actors work representing their ability. CONNECT WITH AFI: ...

Student pleads guilty to accessing records about Frances Abbott design scholarship
The Sydney student who leaked information about a fashion school scholarship controversially awarded to the daughter of the Prime Minister has ...

Accessing a headline opinion
Accessing a headline opinion

Sharp increase in authorities accessing private data
Australian law enforcement and government agencies have sharply increased their access without warrant to vast quantities of private telephone ...

Former librarian charged with accessing student records of Frances Abbott
A former part-time librarian at a Sydney design school has been charged after she allegedly accessed student records of Prime Minister Tony Abbott's ...

Frances Abbott scholarship: Sydney woman pleads guilty to accessing Whitehouse Institute records on PM's ...
A Sydney woman who leaked the student records of Tony Abbott's daughter pleads guilty. A Sydney woman who leaked the student records of Prime ...

Resources last updated: 11/28/2015 5:21:00 PM