Reputation: 771
I want to call a codebehind function from jquery.
The aspx file name is group_master.aspx
The jquery is
function populatecontrol(list, control) {
var id = 0;
var GroupName=document.getElementById('<%=hdnGroupNameCheck.ClientID %>');
if (list.length > 0) {
$.each(list, function (key, value) {
var obj = JSON.parse(value);
document.getElementById('<%=hdnGroupNameCheck.ClientID %>').value=obj.Second;
control.html("Group already exist").append('<a id="editclick" href ="edit('+obj.Second+')">Click here to edit or enquire</a>');
});
}
else
control.text="Group does not exist"
}
The edit('+obj.Second+') is an edit function in codebehind.
Thanks,
Upvotes: 3
Views: 8392
Reputation: 49
You need to make code behind method static and also need to mark it as [WebMethod] so that it will get treated as service method and then uisng jQuery ajax call you can call code behind method like:
var loc = window.location.href;
$.ajax({
type: 'POST',
url: loc + "/GetMessage",
data: "{}",
contentType: "application/json; charset=utf-8"
})
.success(function (response) {
alert(response.d);
})
.error(function (response) {
alert(response.d);
});
Get complete example here : http://www.codegateway.com/2012/05/jquery-call-page-codebehind-method.html
Upvotes: 2
Reputation: 9030
I recommend the use of page methods. In short, you would create a web method in your code behind which would be called by your jQuery logic.
See this link for an example: Page methods in asp.net
Upvotes: 0
Reputation: 46067
This sounds like an ideal candidate for Page Methods
. See this tutorial for more details:
Using jQuery to directly call ASP.NET AJAX page methods
Upvotes: 3