Execute server function since Javascript

Last post 05-12-2008 6:39 AM by Yaler. 6 replies.

Sort Posts:

  • Execute server function since Javascript

    05-07-2008, 1:48 PM
    • Member
      27 point Member
    • Yaler
    • Member since 06-26-2006, 12:09 PM
    • Spain
    • Posts 25

    How I can execute server side function ( result boolean) from javascript code?

  • Re: Execute server function since Javascript

    05-07-2008, 2:26 PM
    • All-Star
      77,103 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,361
    • TrustedFriends-MVPs

    The only ways to execute a server-side method from the client-side (JavaScript) is to either do a PostBack or use AJAX.

    NC...

  • Re: Execute server function since Javascript

    05-07-2008, 2:30 PM
    • Contributor
      6,882 point Contributor
    • pixelsyndicate
    • Member since 07-04-2003, 12:56 PM
    • W. MI transplant in N. TX
    • Posts 1,206

    Like this...

     ASPX Code

    The following valFileName2_ClientValidate() javascript method calls on a C# method called IsFilenameAllowed().
    This was used in conjunction with a custom validator which we wanted to code in C#, but needed to call without postback (due to us using a fileupload control, which cant' exist in an update panel, and would be unwieldy due to the file uploading before validation could take place.

    You can find other examples here: http://www.codeproject.com/KB/aspnet/JavaScriptAndServerEvents.aspx
    and here: http://www.eggheadcafe.com/community/aspnet/17/10010151/calling-c-method-from-ja.aspx

     

    function valFileName2_ClientValidate(source, args)
                   {
    	                // requires an AJAX scriptmanager on your page
    // format is:
    // PageMethods.[serverside_method_name](args.Value, [javascriptcompletemethod],[javascriptfailedmethod], args)
    PageMethods.IsFilenameAllowed(args.Value, valFileName2_OnComplete, valFileName2_OnError, args); } function valFileName2_OnComplete(returnObj, userContext) { for(var i = 0; i < Page_Validators.length; i++) { if(Page_Validators[i].id == "<% = valFileName2.ClientID %>") { if(returnObj) Page_IsValid = false; Page_BlockSubmit = true; Page_Validators[i].isvalid = returnObj; ValidatorUpdateDisplay(Page_Validators[i]); return; } } } function valFileName2_OnError(returnObj, userContext) { }
     

    Code Behind (C#)

    // mark your methods with [WebMethod] to allow javascript accessibility

     

            [WebMethod]
            public static bool IsFilenameAllowed(string filePath)
            {
                // do some stuff here
                return soemthinghere;
            }
     
    Please click 'Mark as Answer' if my reply has assisted you.

    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

    http://wildobson.com
  • Re: Execute server function since Javascript

    05-07-2008, 2:34 PM
    • Member
      27 point Member
    • Yaler
    • Member since 06-26-2006, 12:09 PM
    • Spain
    • Posts 25

    Of corurse i want use AJAX, but how?

    I have a javascript function that open a window modal if a server side function retuns true.

    The server side function execute a complex sql instructions.

    Thanks

  • Re: Execute server function since Javascript

    05-07-2008, 6:59 PM
    Answer
    • Member
      31 point Member
    • rpat03
    • Member since 04-24-2008, 12:27 AM
    • Posts 27

    You can use PageMethod from javascript. You have to make a server side method as Web method.

    Here is the sample code in this link below.

    http://www.geekzilla.co.uk/View30F417D1-8E5B-4C03-99EB-379F167F26B6.htm

     

    Hope this will help.

  • Re: Execute server function since Javascript

    05-07-2008, 8:02 PM
    • Member
      733 point Member
    • uwspstar
    • Member since 03-23-2008, 8:36 PM
    • Milwaukee
    • Posts 210

    Yaler:

    How I can execute server side function ( result boolean) from javascript code?

    hope this one help a little bit

    assume that you have a js function  in you js file

    function CheckFeedBack(id)
    { ...

    // do whatever you want to check

    if (id=='btnSubmit')
        {

           doPostBack('btnSubmit','OnClick');

       }

    }

    and you code you have something like this in your cs page load

    Page.GetPostBackEventReference(btnSubmit);

    if (!Page.IsPostBack)

    {

    btnSubmit.Attributes.Add("onclick", "CheckFeedBack('btnSubmit');return false;");

    }

    ............. 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

    ...//server side function

    }

    founder of AskBargains.com



    If you mark as "Answer"other people can use this answer as a reference
  • Re: Execute server function since Javascript

    05-12-2008, 6:39 AM
    • Member
      27 point Member
    • Yaler
    • Member since 06-26-2006, 12:09 PM
    • Spain
    • Posts 25

    Thanks the pagemethod is a good solution.

Page 1 of 1 (7 items)