Home/ASP.NET Forums/General ASP.NET/jQuery for the ASP.NET Developer/calling code behind method from javascript function

calling code behind method from javascript function RSS

16 replies

Last post Dec 28, 2010 10:54 AM by Saturn888

  • SGWellens

    SGWellens

    All-Star

    126093 Points

    10328 Posts

    Moderator

    Re: calling code behind method from javascript function

    Dec 27, 2010 03:51 PM|SGWellens|LINK

    Saturn888

    I understand it is impossible to directly call a server-side function, but is there a simple way to still fire it?

     

    That is incorrect.

    Here is an example of how to do it.

    http://weblogs.asp.net/stevewellens/archive/2009/06/09/ah-ah-ah-ah-staying-alive-staying-alive.aspx

    Steve Wellens

    My blog
  • Ahmad Kmaldeen

    Ahmad Kmalde...

    Member

    122 Points

    35 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 03:57 PM|Ahmad Kmaldeen|LINK

     have you try it first case it's very fine with me and very simple to use !!!!?

  • Saturn888

    Saturn888

    Member

    13 Points

    66 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 04:02 PM|Saturn888|LINK

    Ahmad Kmaldeen

     have you try it first case it's very fine with me and very simple to use !!!!?

    thanks. I tried but i get the following error:

    "Microsoft JScript runtime error: 'Sys' is undefined"



  • Ahmad Kmaldeen

    Ahmad Kmalde...

    Member

    122 Points

    35 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 04:10 PM|Ahmad Kmaldeen|LINK

    my friend you need to use ScriptManager in the same page

    <asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true">
            </asp:ScriptManager>


     

     

  • Saturn888

    Saturn888

    Member

    13 Points

    66 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 04:23 PM|Saturn888|LINK

    can you show where do you put it?

    Ahmad Kmaldeen

    my friend you need to use ScriptManager in the same page

    <div class="dp-highlighter"><div class="bar"><div class="tools">view plaincopy to clipboardprint?
    </div></div>
    1. <asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true">  
    2. </asp:ScriptManager>  
    </div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true">
            </asp:ScriptManager>


     



  • Ahmad Kmaldeen

    Ahmad Kmalde...

    Member

    122 Points

    35 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 04:29 PM|Ahmad Kmaldeen|LINK

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_Default2" EnableEventValidation="false" %>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
    
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        
    
        <script type="text/javascript">
              var prm = Sys.WebForms.PageRequestManager.getInstance();
        function PostBack(ControlID)
        {
            prm._doPostBack(ControlID, '');
        }
    </Script>
    <asp:Button ID="Button1" runat="server" Text="Invisible Button" Visible="False" onclick="Button1_Click"
             />
    
    
    
    
    <div onclick="PostBack('<%= Button1.ClientID %>');"> this div for test how can javascript post an asp control </div>
    
    
    
    
    
    
        </div>
        </form>
    </body>
    </html>


     

  • Ahmad Kmaldeen

    Ahmad Kmalde...

    Member

    122 Points

    35 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 04:31 PM|Ahmad Kmaldeen|LINK

     

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    
    public partial class test_Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
    
    
        }
    }


  • Saturn888

    Saturn888

    Member

    13 Points

    66 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 05:00 PM|Saturn888|LINK

    thanks again. I still can't see where you can see that the postback is actually working. can you give an example that writes "this is working" for instance?


  • Ahmad Kmaldeen

    Ahmad Kmalde...

    Member

    122 Points

    35 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 05:05 PM|Ahmad Kmaldeen|LINK

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_Default2" EnableEventValidation="false" %>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
    
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        
    
        <script type="text/javascript">
              var prm = Sys.WebForms.PageRequestManager.getInstance();
        function PostBack(ControlID)
        {
            prm._doPostBack(ControlID, '');
        }
    </Script>
    <asp:Button ID="Button1" runat="server" Text="Invisible Button" Visible="False" onclick="Button1_Click"
             />
    
    
    
    
    <div onclick="PostBack('<%= Button1.ClientID %>');"> this div for test how can javascript post an asp control
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </div>
    
    
    
    
    
    
        </div>
        </form>
    </body>
    </html>


     

    And here is the code behind:

     

        using System;
        using System.Collections;
        using System.Configuration;
        using System.Data;
        using System.Linq;
        using System.Web;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.UI.HtmlControls;
        using System.Web.UI.WebControls;
        using System.Web.UI.WebControls.WebParts;
        using System.Xml.Linq;
    
        public partial class test_Default2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                Label1.Text = "And now her it's working";
            }
        }
    


     

     

     

     

     

  • Saturn888

    Saturn888

    Member

    13 Points

    66 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 05:11 PM|Saturn888|LINK

    but the text "And now her it's working" is presented only if the button is actually clicked on the server side. where do I see that the javascript raised the "button1_clicked" event-handler function?

  • Ahmad Kmaldeen

    Ahmad Kmalde...

    Member

    122 Points

    35 Posts

    Re: calling code behind method from javascript function

    Dec 27, 2010 05:22 PM|Ahmad Kmaldeen|LINK

    if that is not what you are looking for so you must want to use the whole page life sycle witouht postback which is the impossible dreame for every asp.net developer but the thing you can use and i think is enough is using webservices which let you do what you want throe ajax but without access your page code behind ...

  • Saturn888

    Saturn888

    Member

    13 Points

    66 Posts

    Re: calling code behind method from javascript function

    Dec 28, 2010 10:54 AM|Saturn888|LINK

    what I want is just a response back to the code behind from the javascript code. what you suggested was a postback from the server side, or I didn't understand it correctly.

    I just thought there would be something relatively simple. postback is what I want, and not a whole get request...

    i have seen blogs talking about a hidden control to which they postback from the javascript code, resembling what you suggested. but I don't understand how such a thing works...

    I need to see one detailed example.


  • ‹ Previous Thread|Next Thread ›