Solved

Pass a Javascript variable to C# server code in ASP.NET

Posted on 2003-10-25
3
512 Views
Last Modified: 2008-01-09
How can I pass a JavaScript variable to the C# code in the server?

If I put a hidden input on the form set to runat=server, it's ID gets mangled when the page is rendered, hence the JavaScript no longer knows the correct ID of the element.

If I don't set the hidden input to runat=server, then it doesn't appear in the Request.Form collection.

I'm fairly stuck on this and I would really appreciate some help.

Thanks.
0
Comment
Question by:Daeljan
3 Comments
 
LVL 10

Accepted Solution

by:
ptmcomp earned 80 total points
ID: 9619445
>If I don't set the hidden input to runat=server, then it doesn't appear in the Request.Form collection.
Something seems to be wrong with your formfields.
It should look like: <input type="hidden" name="preview" value="1">
You can add an id (which must be different from the other controls' id of course: <input type="hidden" id="UniqueId009" name="preview" value="1">
To read it from the form collection you need the name - not the id.

Note: There is also a function Page.RegisterHiddenField for dynamic hidden field creation but since you use javascript, I think it's static.
 
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9624629


What is your ultimate goal?  While I understand your question (pass a client variable to the server), there may be a way to have the variable stored on the server to begin with, and then simply collect the value on an event.


0
 
LVL 2

Author Comment

by:Daeljan
ID: 9625242
Hi ptmcomp,

thanks for restoring my sanity - I think this was a case of not seeing the wood for the trees.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 82
What is "public object" in C#? 3 47
Best resource to learn entity framework 2 33
Where did System.Data.Objects go? 2 27
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question