locked
calling c# method from JvaScript RRS feed

  • Question

  • User2014555709 posted

    Good day.

    In *.aspx.cs

    public partial class select : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    // many strings
    }

    [WebMethod]
    public void strechGMap(int H, int W)
    {
    BigGMap.Height = new Unit(H);
    BigGMap.Width = new Unit(W);
    }
    }

     

    In *.aspx

    <script type="text/javascript">
     function OnSplitterResized(splitter)
    {
    //Get the left pane's client size
    var w = splitter.getRightPane().getWidth();
    var h = splitter.getRightPane().getHeight();


    select.strechGMap(h, w);
    }
    </script>

    Whats wrong? Why function call "select.strechGMap(h, w);" doesnt work?

    Wednesday, September 16, 2009 1:06 PM

Answers

  • User-821857111 posted

    The C# method is on the server.  Javascript runs on the client (the user's browser).  For the Javascript to interact with the web method, you need to use AJAX to run a request.  Even then, the C# method needs to have a return type other than void, so it returns some values for the javascript to work with.  Although these examples call a server-side method that interacts with a database using jQuery, the basics are the same as you are trying to achieve: http://www.mikesdotnetting.com/Article/104/Many-ways-to-communicate-with-your-database-using-jQuery-AJAX-and-ASP.NET

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2009 3:21 PM

All replies

  • User-821857111 posted

    The C# method is on the server.  Javascript runs on the client (the user's browser).  For the Javascript to interact with the web method, you need to use AJAX to run a request.  Even then, the C# method needs to have a return type other than void, so it returns some values for the javascript to work with.  Although these examples call a server-side method that interacts with a database using jQuery, the basics are the same as you are trying to achieve: http://www.mikesdotnetting.com/Article/104/Many-ways-to-communicate-with-your-database-using-jQuery-AJAX-and-ASP.NET

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2009 3:21 PM
  • User-2070791419 posted

      1. use PageMethods instead of select.

         The js call should be

       PageMethods.strechGMap(h, w);


    2. the web method should public static

    3. You need EnablePageMethods="true" on your ScriptManager.
    Wednesday, September 16, 2009 3:22 PM