Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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

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.
0
saabStory
Asked:
saabStory
  • 3
  • 2
2 Solutions
 
vb_studentCommented:
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.
0
 
saabStoryAuthor Commented:
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.
0
 
vb_studentCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Saqib KhanSenior DeveloperCommented:
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'

}
0
 
saabStoryAuthor Commented:
adikhan,
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);"

0
 
saabStoryAuthor Commented:
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.
 
 
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now