Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created one control which has one function but I want to call that function with ajax call on any .aspx page or master page same as like calling function from a code behind file,am fine with codebehind file but stucked with calling function from acsx.cs file to any .aspx page/master page on which am including/registering that control.
this is my HTML
<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <input type="button" id="Button2" runat="server" value="Button" />
</div>

my ascx.cs code
[WebMethod()]
[ScriptMethod()]
public static void myMethod(string text)
{
  Label1.text=text;
}

my ajax call whn am calling function from code behind file is like
$(document).ready(function () {
    $("#Button2").click(function () {
        var text = $("#TextBox1").val();        
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/myMethod",
            data: "{ text: '" + text + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: "true",
            cache: "false",
            success: function (msg) {
                alert("success");
            },
            Error: function (x, e) {
                // On Error
            }
        });
    });
    });

in ajax call am calling function from codebehind thats why am puting url: "WebForm1.aspx/myMethod", but am confused when about calling the function from .ascx.cs, like what will be a Url value for that ,because am simply using that control on my page like
<UC:userControl id="control1" runat="server" />

which I have included/registered on my page like
<%@ Register TagPrefix="UC" TagName="userControl " src="~/Controls/Mycontrol.ascx" %> 


What I have tried:

how do i resolved this issue?
i tried url value(s) like
<pre> url: "Mycontrol.ascx/myMethod",

also
<pre> url: "WebForm1.aspx/Mycontrol.ascx/myMethod",


but still am unable to get it
Posted
Updated 11-Jan-17 8:19am
v2

1 solution

You can't read from server-side controls or update their values from a webmethod so what you're trying to do is never going to work. To answer the question regardless if the method is to be called from anywhere you're better using an ashx handler than a webmethod. Google "call ashx handler from jquery" for implementation examples.
 
Share this answer
 
Comments
Madhav Gunjal 5-Jan-17 1:56am    
here i have a user control so how do i implement this thing with user control and master page?
F-ES Sitecore 5-Jan-17 4:17am    
You can't embed web methods in an user control on a page
Madhav Gunjal 5-Jan-17 6:53am    
so is there any aother way to get thsi?because am going to use that control on master page.
F-ES Sitecore 5-Jan-17 6:59am    
The way around it is to use an ashx handler instead. Your dogged instinance that this should happen in your ascx component has me suspecting you are looking to access server controls which, I will re-iterate again, is not possible with webmethods. Your code should always be in the most appropriate place and if that method is to be called from any client page then its place is not inside a user control inside a master page, but instead somewhere global such as it's own handler.
Madhav Gunjal 5-Jan-17 8:08am    
i came to this just because when am clicking on button which is in my control then that click action reloads entire page,so many people suggest meajax call and all that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900