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 anticipation

Harshad

 

0
test37
3/14/2009 3:26:11 AM
asp.net.client-side 24353 articles. 2 followers. Follow

6 Replies
2219 Views

Similar Articles

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

Yes it is possible to get the value of your javascript variable in C#. here's how:

1. In javascript suppose you have a variable called x.

2. Add a hidden server-control to the page:
<input type="hidden" id="hid" name="hid" runat="server" />

3. In javascript assign the value of x to hidden control as

document.getElementById("<%= hid.ClientID %>").value = x;

4.  On the server side code in C# when you can access the hidden variable value containing value of x as:

string str = hid.value.ToString();

Hope this helps.


AJ
Please don't forget to click “Mark as Answer” on the post that helped you.
0
ashishnjain
3/14/2009 3:59:40 AM

 Thanks a lot Ashish.

 I am using  Component Art Combobox with runat "Callback". I tried the way you suggested but i am not receiving value from server side. It showing error on the name of hidden variable in C#. If I use Input submit button i am getting value on server side but i don't want to use button event  on change selection of combobox i am creating string that string i want to pass along with index change. Means i want to send two information one is value of changed index and other is string which i generated at the time of changed event. Changed selection event woking fine but i want to send string from javascript to c#(server side).

 I can send two string combine but on server side there is other event data_request that stop working if i send information of selection changed it working fine. My idea is in data_reqest event i want access the string which is generated in javascript from C#.

Please show me way i stuck.Tongue Tied

Once again thanks

Harshad Gandhi

0
test37
3/14/2009 4:03:06 PM
Hi test37
 
Based on my understanding, you are working with the third-party control ComponentArt, right?
 
If so, I would like to suggest you check their support center:
 
http://www.componentart.com/support/
 
Because this is an ASP.NET forum, and community members here may not familiar with that control.

Thanks.


Lance Zhang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Lance
3/18/2009 3:36:47 AM

 1. I have Javascript variable I want to use in Server side C# is it possible ?

answer : the bridge b/w the javascript and the serverside are the Html Form Fields which are available to both (client and server), so put your javascript values inside hidden(or not hidden)   form fields and use them on the server side by.

 2. If above is not possible if i declare variable in C#  and is it possible to update through Javascript ?

answer : same as above..


SHASHANK BHIDE
CAPGEMINI INDIA
"THE ROAD TO SUCCESS IS ALWAYS UNDER CONSTRUCTION MARK MY RESPONSE AS ANSWER TO HELP ME BUILD IT :)"
0
shashankgwl
3/18/2009 10:07:07 AM

test37:
1. I have Javascript variable I want to use in Server side C# is it possible ?

For that you can use Server side tags and assign the value from server to javascript variable

Eg

var temp = '<%=str%>';

where str is a server side variable

test37:

2. If above is not possible if i declare variable in C#  and is it possible to update through Javascript ?

But Updating a a normal variable can be done only using a postback if you want to update session variable you have an optionto make a asynchronous call and do it


MAKMark as Answer if this reply helps you
MVP ASP/ASP.Net
MVP ASP/ASP.Net
ASP.Net Hosting : Host DepotMy Site : ASPSnippets
0
mudassarkhan
3/18/2009 10:33:29 AM

Hi,

U can use classical
http://www.w3schools.com/ajax/ajax_database.asp

or u can use Ajax.net

Hope it helps


Thanks
Avinash Tiwari

Remember to click “Mark as Answer” on the post, if it helps you.
0
qwe123kids
3/18/2009 10:36:59 AM
Reply:

Similar Artilces:

Setting the value of server side variable in javascript and wants to use that server side variable on page load.
Hi, I am trying to set the value of a server side variable in javascript and wants to use that variable on page load.  Here is my sample code. -------------------------- private string testVal = null;  private void registerScript(){ if (!this.Page.ClientScript.IsClientScriptBlockRegistered("RequestHeaderValidation")){ StringBuilder sb = new StringBuilder("" + Environment.NewLine);sb.Append(" var version = '2.0'; " + Environment.NewLine); sb.Append(" var isInstalled = Silverlight.isInstalled(version);" + Environment.NewLine)...

server side variables in client side javascript
I want to use an ASP.net 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 www.Interdevelopments.com and check the future of open mobile widget solutions at www.glowe.org 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...

Can you call a c# on the server side from javascript functions on client side
Hello guys This is something intresting I have come across during development. I have a javascript written in a .JS file , to my knowledge the .js files are executed on the server side. But security concerns with java script that these .js files can be access by connecting to port 80 of the server . So an ultimate alternative reminas to call c# code from a java script function. Can anyone Help me on this matter Regards Nihar Hi Nihar, Usually .js files executed on client side. Please explain your configuration.Leon LangleybenMCSD, ASP.NET MVPBlog...

can we assign the javascript variable with the server side variable ?
Hi, can we assign the javascript variable with the server side variable likelike  Var web :=<%=Request.url%>?  other wise what is the another way?  Can't think why that would not work.  What happens when you try it? Although if you need to access the URL via JavaScript you can always use window.location.href Please mark helpful posts as "Answer" I would store your server-side value in a HiddenField control.  Then you can access and modify the value from both ends. Hope this helps!  Please mark the helpful post(s) as An...

How to call server side(C#) function from Client side(Javascript) function?
Hi i have used below functions,  function HandleOnClose() {// this function will be called after clicking browsers close button in Internet explorer if (event.clientY < 0) { event.returnValue = 'Are you sure you want to leave the page?'; } // if he clicks ok then i need to call one server side function in this instance.. else not. } <body onbeforeunload="HandleOnClose()" leftmargin="0" rightmargin="0">other controls in page..</div>  How to achieve this? Thanks MS AJAX Extensions would be the option of choice here. Combined with webservices you can...

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

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

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

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

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 (Asp.net) 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...

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

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

how to call server side function from client side javascript?
Hi all, I have a image button in the update panel. I would like to call a method from server side (not from webservice) to get the variable when Onclick on the image button is invoked. Is there any way that I can able to access this function from the client script? In using the UpdatePanel, in essence, you're already doing this because there will not be a full postback.  You can make an asynchronous call to your Click method of your ImageButton.  So, what do you want to do with this "variable"?Christopher Reed"The oxen are slow, but the earth is patient."...

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

Web resources about - Updateting Server side variable through javascript or how can i user Javascript variable in server side C# ? - asp.net.client-side

Resources last updated: 12/18/2015 2:36:59 PM