I create a new WebForms Application in VS2013. I didn't change anything and created a simple page. I want to load this table on client side when user clicks button
<table id="tblHelpRow">
<thead>
<tr class="title">
<th>F2
</th>
<th>F3
</th>
</tr>
</thead>
<tbody id="helpRowBody">
<%=MyRow %>
</tbody>
</table>
<asp:LinkButton ID="lnkEdit" runat="server" onclick="fnEdit();" />
This is my script code:
function fnEdit() {
BindGridView();
};
function BindGridView() {
rowid = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: param,
dataType: "json",
success: function (data) {
alert(data);
}
});
}
I have a WebMethod in my code-behind which stores result in the public property. DataSource I store in session, but rowID I need to pass from jquery.
[WebMethod]
public static string GetRow(int rowID)
{
DataTable dt = (DataTable)HttpContext.Current.Session["SourceData"];
MyRow = "<tr>" +
"<td>" + dt.Rows[rowID]["F2"].ToString() + "</td>" +
"<td>" + dt.Rows[rowID]["F3"].ToString() + "</td>" +
"</tr>";
return "done";
}
But I don't get any result. When I have put the breakpont in success I got "Authentication failed" error and this webmethod wasn't executed. What's the problem? I didn't change ant Authentication settings.
Try removing the ScriptMethod attribute. You are specifying a POST action type but I believe the ScriptMethod forces the request to be a GET by default. Also, I believe your param needs to be a JSON string instead of just an integer:
var param = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
dataType: "json",
success: function (data) {
alert(data);
}
});