UncleCoder.com

UncleCoder.com

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: 1783

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

 

DEMO

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

 

Productdetail.aspx

 

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

<script>

 function buttonClick() {
                var counter = 0;
                var url = window.location.pathname;
                for (i = 0; i < url.length; i++) {
                    if (url.charAt(i) == '/') {
                        counter++;
                    }
                }
                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)
            {
                alert(response);

            }

</script>

 

 

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

Global.asax

 protected void Application_Start(object sender, EventArgs e)
        {
            RegisterCustomRoutes(RouteTable.Routes);

        }

 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.

 

ProductDetails.aspx.cs

 

 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;

        }

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:


Advertisement