Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have alrady tried with the Pagemethods.Method1
But for me, i need to call an serverside event eg)
JavaScript
function myFunction() {
           PageMethods.Method1();
       }

public static void Method1()
{
class clss1 = new class()
class1.txtBox1_TextChanged(new object(), new EventArgs());
}

protected void txtBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = "";
panel1.Visible = true;
}


Here, txtBox1_TextChanged() is getting triggred but the values of the page controls are always getting as null.


Please help me to solve this issue.

Thanks in advance.
Posted
Updated 27-Oct-14 4:45am
v2

1 solution

add [System.Web.Services.WebMethod()] to your code like this

C#
[System.Web.Services.WebMethod()]
public static void Method1()
{
class clss1 = new class()
class1.txtBox1_TextChanged(new object(), new EventArgs());
}

or use jquery ajax method
JavaScript
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "yourpage.aspx/yourmethod",
data: "{}",
dataType: "json",
success: function(data) {
//Write functionality to display data
},
error: function(result) {
alert("Error");
}
});


More info from here and here
 
Share this answer
 
v2
Comments
[no name] 27-Oct-14 12:34pm    
I have already tried with the 1st approach you mentioned and as i said before i can able to hit the method but since we create a static method, we have to create a new object and call the particular method, but then the values of the page controls are getting as null
Dilan Shaminda 27-Oct-14 12:59pm    
use the second approach.a example can be found here...
http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
[no name] 28-Oct-14 2:58am    
tried that one also but no luck.
Dilan Shaminda 28-Oct-14 3:04am    
http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
it has a working sample..
[no name] 28-Oct-14 3:50am    
I followed the same way that articles tells and the method was getting called and the page controls are still returning as null.

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