Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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