How to pass a javascript variable onClick to a .net hidden text box??

Posted on 2010-01-08
Medium Priority
Last Modified: 2012-05-08
I'm new to .net and just can't find the right way to do what I want.  It's frustrating because I can do this in a number of other languages but that doesn't translate too well with dot net.

What I have is a link with an onClick event that will pass a number to a hidden field on my dot net page whereupon the page reloads and the value in the hidden field is used to load the new page based on that passed ID number.

But I can't figure out the connection between javascript and dotnet.  Plus I'm using a master page and I think that is complicating matters.

The field is a hidden dot net text field named txtProjectID.  So how to I get the variable passing from the onClick to the text field - I'm using C# by the way.

Thanks in advance for help with such a simple question.
Question by:saabStory
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
  • 2

Expert Comment

ID: 26248624
web java and .net have issues blending i feel your pain. i would use the onclick method to link directly to a .net method. this is easy if it is ok to let the onclick method run at post back. visual studio supplies these events for most controls. it dose get more complicated the more you do on the client side but if you don't need it to bee a clients side method you can remove all java. hope i am of service.

Author Comment

ID: 26249887
True enough. This is a part of a menu and the onClick is in the sub menus.  I tried but wasn't able to use the built in menu controls as they wouldn't work with the design - so that ruled out the easy way.  So I went with a dhtml/xml menu.  Hence the javascript onClick.

Accepted Solution

vb_student earned 1000 total points
ID: 26250571
you can call .net methds with javascript it is just a little harder. you can type a java comand like call new_sub() and if you have a sub with that name in you .net file it will call it. so you .net code could be.

public sub whatever
end sub

then your java can call this and this sub can do the work. sorry i don't have the syntax for c# or java mastered. but hopfully this will be a good example.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 23

Assisted Solution

by:Saqib Khan
Saqib Khan earned 1000 total points
ID: 26261958
simply add the onClientClick attribute to your Link Tag

<asp:hyperlink onClientClick="myFunction()" .... />

now in javaScript Function

function myFunction() {

 fld = document.getElementById("txtProjectID").value = 'your value'


Author Comment

ID: 26272483
Sorry, I'm not getting the function to work.  Depending on how I write it, the error is either the value or the getElementById is null or there's something wrong with the left side assignment.

To be clear, I'm not using an asp:hyperlink for the onClick.  It's in an XML file that is fed into the DHTML menu - so the syntax is plain javascript - "onClick= changeProject(1);"


Author Comment

ID: 26272549
Thanks guys - I was being an idiot.  I forgot that dotnet changes the variable name at runtime.  While the field was txtProjectID in the code, it changed to ctl00_main_txtProjectID when I ran it.  Works fine now.
Thanks for you help.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…
Suggested Courses

764 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