Free programming examples and instructions

PageMethods from RoutedUrl

code and demo for how to call webmethods from routed url with url params

by Athil

Posted on 26 Feb 2017 Category: Asp.net Views: 2143

Edited on 02 Jun 2017

Here I am going to show how calling PageMethods from routed url for that I am going create a page showing product details



PageMethods.set_path(path + '/Productdetails.aspx');

scriptmanager takes each slashes in the url as folders, So we need to create slashes as number of slashes in the url. I write code for taking slashes automatically. please check bellow




 <form id="form1" runat="server">
        <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>

Script Section


 function buttonClick() {
                var counter = 0;
                var url = window.location.pathname;
                for (i = 0; i < url.length; i++) {
                    if (url.charAt(i) == '/') {
                var path = "";

                for (i = 1; i < counter ; i++) {
                    path += "../";
                PageMethods.set_path(path + '/Productdetails.aspx');

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





Here I addedd a code 'PageMethods.set_path(path + '/Productdetails.aspx');' for getting the path where the server side code is existed . In routed Url there may some parameters in Url so the slashes(/) are not for showing folders so we need to define the currect path by putting slashes or script manager will check identify it as folder and check for that folders. To identify number of slashes existed and automatically set the path I created a code in script tag that you can easily understand what I mean


 protected void Application_Start(object sender, EventArgs e)


 void RegisterCustomRoutes(RouteCollection routes)
            routes.MapPageRoute("ProductDetail", "ProductDetails/{Id}/{Code}/{Name}", "~/ProductDetails.aspx");


I Global.asax file aI added these codes for url routing, Here Id,Code,Name were parameters.




 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;

        public static string GetName(string txtValue)
            return DateTime.Now + txtValue;


here I added a load event to show how I take values from ulr and for button click event I add a separate function 

Leave a Comment: