×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Referencing values in html form or java script in code behind

Referencing values in html form or java script in code behind

Referencing values in html form or java script in code behind

(OP)
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

RE: Referencing values in html form or java script in code behind

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

----------------------------------------------------------------------------------------
http://www.whatsthehampton.com/

RE: Referencing values in html form or java script in code behind

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

RE: Referencing values in html form or java script in code behind

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

RE: Referencing values in html form or java script in code behind

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" />

RE: Referencing values in html form or java script in code behind

well from that angle you are correct...

Known is handfull, Unknown is worldfull

RE: Referencing values in html form or java script in code behind

(OP)
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?

RE: Referencing values in html form or java script in code behind

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 (http://www.tek-tips.com/search.cfm) or read FAQ222-2244 on how to get better results.

RE: Referencing values in html form or java script in code behind

(OP)
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

RE: Referencing values in html form or java script in code behind

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

Known is handfull, Unknown is worldfull

RE: Referencing values in html form or java script in code behind

Well I can think of disadvantages for all of those methods:

Quote:


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

Quote:


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

Quote:


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 (http://www.tek-tips.com/search.cfm) or read FAQ222-2244 on how to get better results.

RE: Referencing values in html form or java script in code behind

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)

RE: Referencing values in html form or java script in code behind

Quote:


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 (http://www.tek-tips.com/search.cfm) or read FAQ222-2244 on how to get better results.

RE: Referencing values in html form or java script in code behind

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close