Solved

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

Posted on 2003-10-25
3
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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