JavaScript Calls C# Function to access a Database

Last post 06-29-2007 9:20 AM by JoshStodola. 20 replies.

Sort Posts:

  • JavaScript Calls C# Function to access a Database

    06-25-2007, 3:48 PM

    I am trying to write a client side script for a custom validator.   The client side function will be in javascript; however, the script will need to use a previously created member function from a C# class I created.  This C# function accesses a database to retrieve the count of records for a particular table. I haven't written much javascript so I am sure of the best way to implement this script.  Is this possible?  If so, how?

    Fox
  • Re: JavaScript Calls C# Function to access a Database

    06-25-2007, 4:06 PM

    Ajax.

    You can either roll your own, or you can use ASP.NET Ajax

     

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: JavaScript Calls C# Function to access a Database

    06-25-2007, 4:08 PM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    I would create a hidden field on your form, and populate it with your needed count on the server-side Page_Load.  Then your validation can compare any value against the hidden field's value. 

    Just remember if you are going to do a numerical comparision to use the javascript parseInt() method so that it wont do a string comparison.

    This should work fine, unless this count from the database needs to be accurate up-to-the-second, in which your only hope will be to use AJAX.

    Hope this helps!

    Josh Stodola ← Come check out my blog!
  • Re: JavaScript Calls C# Function to access a Database

    06-26-2007, 1:29 AM

     I am trying JoshStodola hidden field suggestion, but I can't seem to get the javascript to work.  The script is below:

     

     function PrimeKeyCheck(sender, args)
      {
       var numberOfRecords;
        var hiddenField = document.getElementById("NumOfRecordsHiddenField");
        numberOfRecords = parseInt(hiddenField);
          if (numberOfRecords > 0)
          {
             args.IsValid = false;
             return;
          }

        args.IsValid = true;
        return;

      }

    I am trying  to locate the hidden field NumOfRecordsHiddenField on the form to get the values it stores. I am not sure what I am doing wrong.  Any suggestions?

    Fox
  • Re: JavaScript Calls C# Function to access a Database

    06-26-2007, 10:08 AM
    Answer
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    Almost!!

        numberOfRecords = parseInt(hiddenField.value);
        alert(numberOfRecords);

    If that still doesnt correct your problem, you might need to post some more code, and also put an alert to display the hiddenField var to see if it is able to locate the hidden field.

    Hope this helps!

    Josh Stodola ← Come check out my blog!
  • Re: JavaScript Calls C# Function to access a Database

    06-26-2007, 9:16 PM

     I tried your suggestion, but the script still doesn't seem to be working correctly.  I underlined the changes I made to the script:

     function PrimeKeyCheck(sender, args)
     {
            var numberOfRecords=0;
            var hiddenField = document.getElementById("<%=NumOfRecordsHiddenField.ClientID%>").value;
     
            numberOfRecords = parseInt(hiddenField.value);
            alert(numberOfRecords.value);
             if (numberOfRecords > 0)
            {
                 args.IsValid = false;
                return;
            }

        args.IsValid = true;
        return;

        return ;
      }

     

    I think it located the hidden field but is as if the control has no value or there is something wrong with the value.   Also, thanks for the help so far.  Any more suggestions on what could be wrong?

    Fox
  • Re: JavaScript Calls C# Function to access a Database

    06-27-2007, 8:38 AM

    I think it located the hidden field but is as if the control has no value or there is something wrong with the value.  The value stored in the hidden field is saved from a textbox during the textbox's TextChanged event.  I tested to ensure that the value is being saved to the hidden field (NumOfRecordsHiddenField).  I think the script is called before the TextChanged event occurs. So that may be my problem.  Thanks again, JoshStodola.

     

    P.S. This script is for a client side validation that I am using with a Custom validator to display a ValidatorCallout.  From what I read the ValidatorCallout only works with a Custom Validator when using a client side validation. 
    Fox
  • Re: JavaScript Calls C# Function to access a Database

    06-27-2007, 9:08 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    Try the following function:

    function PrimeKeyCheck(sender, args)
     {
            var numberOfRecords=0;
            var hiddenField = document.getElementById("<%=NumOfRecordsHiddenField.ClientID%>");
            alert(hiddenField);
            alert(hiddenField.value);
     
            numberOfRecords = parseInt(hiddenField.value);
            alert(numberOfRecords.value);
             if (numberOfRecords > 0)
            {
                 args.IsValid = false;
                return;
            }

        args.IsValid = true;
        return;

        return ;
      }

    And tell me what it says in the two alert boxes.

    Thanks

    Josh Stodola ← Come check out my blog!
  • Re: JavaScript Calls C# Function to access a Database

    06-27-2007, 2:16 PM

     

    After entering text into the validation textbook, the script fires and produces the alerts below: 

      1st alert alert(hiddenField) it shows:

      alert one

     

    2nd alert alert(hiddenField.value) it shows:

    alert two 

     3rd alert alert(numberOfRecords.value)

     

     


    Fox
  • Re: JavaScript Calls C# Function to access a Database

    06-27-2007, 2:40 PM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    OK, the third alert is undefined becusae you are calling .value of something that is already the .value, so get rid of that.  It is finding the hidden element, but as you can see it is empty.  So, you are having problems with populating the hidden field with your data.

    Josh Stodola ← Come check out my blog!
  • Re: JavaScript Calls C# Function to access a Database

    06-27-2007, 3:27 PM

     Thanks for the help, man. We tried.  I will try to implement the client callback features suggested in ASP.NET 2.0's Client Callback Feature now. I think this article will explain how to setup a client function to callback to a particular server function.

    Fox
  • Re: JavaScript Calls C# Function to access a Database

    06-27-2007, 3:34 PM

    Shea L. Fox:

     Thanks for the help, man. We tried.  I will try to implement the client callback features suggested in ASP.NET 2.0's Client Callback Feature now. I think this article will explain how to setup a client function to callback to a particular server function.

     

    Ajax, in other words. 

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: JavaScript Calls C# Function to access a Database

    06-28-2007, 10:18 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    Well, I think using AJAX for this scenario is unnecessary, and almost silly.  You are sooo close to having the hidden field method work.  Please post the code that populates the "count" into the hidden field.

    Josh Stodola ← Come check out my blog!
  • Re: JavaScript Calls C# Function to access a Database

    06-28-2007, 10:46 AM

    JoshStodola:

    Well, I think using AJAX for this scenario is unnecessary, and almost silly. 

     

    Do you?  I think it's perfect for validation.

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: JavaScript Calls C# Function to access a Database

    06-28-2007, 11:05 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    The vast majority of validation can be done without making an extra trip to the server.  Which, correct me if I am wrong, is the entire prupose of validation!

    Josh Stodola ← Come check out my blog!
Page 1 of 2 (21 items) 1