Results 1 to 6 of 6

Thread: Declaring a code-behind variable for Javascript?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Declaring a code-behind variable for Javascript?

    What I am trying to do, is perform some edit checks on required fields in my page. When the "Submit" button is clicked, I wish to check execute a javascript function that does these checks.

    The way I'm envisioning it is to declare a server-side variable in my VB.Net code-behind, set this value in the javascript CheckEdits() function and then back in the code-behind in the btnSubmit_Click() Event, check this variable.

    Problem is...I'm not sure how to declare a code-behind boolean variable that can be checked in a javascript function. Any ideas?

    Thanks,
    Blake

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Declaring a code-behind variable for Javascript?

    You could do it using Ajax, if the validation absolutely must be server-side. However, if the checks are simple "required" or "numeric" and the like, you can do that in JavaScript without the need for messy server-side checks. Which one do you need?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Declaring a code-behind variable for Javascript?

    Well, I the fields to be validated upon clicking the "Submit" button. So it sounds to me like it would be server-side validation. Doesn't a button, when clicked automatically make a request to the server? I would like to be able to click this button and process it using the client only. All this form does is validate some input and then sends an SMTP email (which I haven't gotten to work yet).

    Thanks,
    Blake

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Declaring a code-behind variable for Javascript?

    Not necessarily. JavaScript can easily override form submit events. What are your validation criteria? Whether you can do this client-side depends on that.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Declaring a code-behind variable for Javascript?

    The form consists of 5 fields:

    1. Users Name
    2. Organization Name
    3. Users Email Address
    4. Subject
    5. Memo/Note

    All 5 fields have to be filled out. Pretty simple validation!
    Blake

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Declaring a code-behind variable for Javascript?

    Here's an example of how to do that. Note that you shouldn't use alerts to tell people that a field is missing, however - that's just a demo. If you don't know JavaScript, you should find a good tutorial online or a book, and you can also get help in the right forum for this, which is Web Development

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width