UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Page methods from routed URL - ASP.net

Demo and instructions for how to implement pagemethods from routed URL in ASP.net

by Athil


Posted on 27 Jul 2018 Category: Asp.net Views: 4099


Here I am going to show how to implement pagemethods from routed URL asp.net.

DEMO

script manager consider each slash as folders, So we have to go back folder with the number of slashes in URL.

<script>
PageMethods.set_path('../../../Productdetails.aspx');
</script>

OR before calling the function use ResolveUrl method

<script>
PageMethods.set_path( '<%= ResolveUrl("~/") %>/Productdetails.aspx');
</script>

 

The bellow code will take URL and identify number of slashes and give the currect path to  script manager


 function buttonClick() 
 {
         
      PageMethods.set_path( '<%= ResolveUrl("~/") %>/Productdetails.aspx');
      PageMethods.GetName(document.getElementById('txtName').value, onSuccessGetName);
 }
 function onSuccessGetName(response, userContext, methodName)
 {
      alert(response);
 }

Demo Code 

On Client Sode

ASPX

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
      <div>
        <br />
       <h3> Demo of calling web method from routed url with url params </h3>
        <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>
        Id: <asp:Label ID="lblId" runat="server" Text=""></asp:Label> <br/>
        Name : <asp:Label ID="lblName" runat="server" Text=""></asp:Label> <br/>
        Code : <asp:Label ID="lblCode" runat="server" Text=""></asp:Label> <br/>


        Plz Enter you name : <input type="text" ID="txtName" runat="server"></input>
        <asp:Button ID="btnClick" runat="server" OnClientClick="buttonClick();" Text="Button" />


    </div>
    </form>
</body>

        </div>

</html>

javascript


  
            function buttonClick() {
               
               PageMethods.set_path( '<%= ResolveUrl("~/") %>/Productdetails.aspx');



                PageMethods.GetName(document.getElementById('txtName').value, onSuccessGetName);
            }
            function onSuccessGetName(response, userContext, methodName)
            {
                alert(response);

            }

Server side

protected void Page_Load(object sender, EventArgs e)
        {
            string Id, Name, Code;
            if (Page.RouteData.Values["Id"] != null)
            {
                Id = Page.RouteData.Values["Id"].ToString();
                lblId.Text = Id;
            }
            if (Page.RouteData.Values["Name"] != null)
            {
                Name =   Page.RouteData.Values["Name"].ToString();
                lblName.Text = Name;
            }
            if (Page.RouteData.Values["Code"] != null)
            {
                Code = Page.RouteData.Values["Code"].ToString();
                lblCode.Text = Code;
            }
        }
        [System.Web.Services.WebMethod]
        public static string GetName(string txtValue)
        {
            return DateTime.Now + txtValue;

        }

 



Leave a Comment:


Click here to register

Popular articles