Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-10-25
3
Medium Priority
?
523 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 320 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

783 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