Solved

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

Posted on 2003-10-25
3
507 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
Comment Utility
>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_
Comment Utility


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
Comment Utility
Hi ptmcomp,

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now