Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing values in html form or java script in code behind 1

Status
Not open for further replies.

developer155

Programmer
Joined
Jan 21, 2004
Messages
512
Location
US
I have aspx page with HTML form that calls JavaScript procedure OnSubmit (the java script gathers all the values in the form and puts them into a cookie). I need the values in that HTML form (preferable from javascript procedure) in the code behind to process them (call store procedure with them).
So my question is is it possible to pass the values to code behind from java script function after form has been submitted? Or do I have to change the form to server side controls and get the values that way in my code behind?

thanks
 
Hi,

Well that's what .net forms are for - to post back to the server.
If you need to process the values on the server, then no need to gather the data with javascript.
Make sure your form has a 'runat=server' tag and then use asp.net textboxes etc.

asp.net makes this really easy.

hth
j

----------------------------------------------------------------------------------------
 
Code:
<head>
function gather(){
var del=String.fromCharCode(160)
var a='hola'
var b='hello'

var ret=a+del+b
document.forms[0].myjavascriptvars.value=ret
}
</head>
<form onsubmit="gather();return true" runat=server>
<input type=hidden name=myjavascriptvars>
Code:
sub page_load()
dim arr() as string=request.form("myjavascriptvars").ToString.Split(chr(160))
end sub
 
or:
<head>
function gather(){
var del=String.fromCharCode(160)
var a='hola'
var b='hello'

var ret=a+del+b
document.forms[0].myjavascriptvars.value=ret
}
</head>
<form onsubmit="gather();return true" runat=server>
<input type=hidden name=myjavascriptvars id=myjavascriptvars runat="Server">


code behind file:
Private WithEvents myjavascriptvars as HtmlInputHidden
sub page_load()
response.write(myjavascriptvars.value)
end sub

Known is handfull, Unknown is worldfull
 
Vbkris, that will work if it is used in a simple webform.
It is very common (an good practice) to put your work in Web User Controls.

When you do, the client side ID is not going to be myjavascriptvars because the ID and Name will change.

This is what is looks like on the client:
<input name="Control1:myjavascriptvars" id="Control1_myjavascriptvars" type="hidden" />
 
well from that angle you are correct...

Known is handfull, Unknown is worldfull
 
Thanks for all your responces: One question about
sub page_load()
dim arr() as string=request.form("myjavascriptvars").ToString.Split(chr(160))
end sub

why is the code in page_load function? when is page_load called? after the user submits the form?

 
The page load event fires when the page is loaded (funnily enough!).

If you click a button to cause a postback, you will see if you step through each event that the page load event fires before the button click event (so you could place it in either event depending on what you need to do with the data).

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
so what method do you think is better for gathering values from a form and passing them to code behind:

with java script and hidden fields OR

converting form and controls to server side and capturing using input directly from code behind OR

gathering the values with java script and creating a cookie and reading the cookie in the code behind (Request.Cookies..) which is the current implementation


thanks

 
that would actually depend on the situtation. i have never used the third method...

Known is handfull, Unknown is worldfull
 
Well I can think of disadvantages for all of those methods:

with java script and hidden fields
What happens if javascript is disabled on the client?

converting form and controls to server side and capturing using input directly from code behind
RTomes pointed out the drawback if they are implemented within a User Control

gathering the values with java script and creating a cookie and reading the cookie in the code behind
What if the client won't accept cookies?

There are always going to be certain hurdles when dealing with client & server side values but in this case I would have said the method described by RTomes was probably best suited to this situation. You will have to make your own mind up though as it really depends on the implementation of the page and the users that will be using the site.



--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Someone that turns off JavaScript will cause major problems in almost all the websites out there. The user that does this is going to get frustrated in the first week and turn JavaScript back on.
Turning of cookies is much more common and does not effect the functionality nearly as much as turning of all JavaScript (menus, button mouse overs, ..., the list goes on and on with JavaScript)
 
Someone that turns off JavaScript will cause major problems in almost all the websites out there. The user that does this is going to get frustrated in the first week and turn JavaScript back on.
It's not always the case that the user turns javascript off - we have clients that use hand held devices and these only have text based browsers which don't support javascript so we have cases where we have to cater for these users.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I have had simular concerns. After considering it I realized if JavaScript is turned off then the
function __doPostBack(eventTarget, eventArgument) {
will not work and that disables half of the .net controls.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top