February 21, 2010 by Christoff Truter C# ASP.NET
PageMethods in Ajax.net allow us to call server-side methods (e.g. C#, VB.net) from within
client-side functions (e.g. JavaScript).
There is one limitation I would like to point out, before continuing with my post - the fact that
one can't access webcontrols (e.g. GridViews, DropDownList), from within the server-side
method we're calling.
Which is an obvious limitation; in regards to maintaining ViewState, ControlState, re-rendering of
controls etc - since we're working with which
is essentially dead objects. (We'd need to pass state to our method, rebuild our page with its various
control collections re-instate it with our background request (Ajax), eventually altering the current page in
view, including its various states - potentially defeating the whole purpose of what Ajax is all about, since
we'd simply be recreating postbacks, not very effective)
Note: we can however access the rendered controls client-side using the DOM, which will be demonstrated
in this post.
In this post, we're going to create a crude little web application, that's going to look something like this:
<asp:ScriptManager runat="server" ID="smAjax" EnablePageMethods="true"></asp:ScriptManager>
[WebMethod]
public static void delete_lbxFriends(Int32 friendID)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("removeFriend", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@friendID", friendID);
command.ExecuteNonQuery();
}
}
}
function deleteListItem(sender, e)
{
if (e.keyCode == 46) {
var friendID = sender.options[sender.selectedIndex].value;
sender.disabled = true;
PageMethods.delete_lbxFriends(friendID, succeededDeletedCallback, failedDeletedCallback, sender);
}
}
function succeededDeletedCallback(result, userContext, methodName)
{
userContext.remove(userContext.selectedIndex);
userContext.disabled = false;
}
function failedDeletedCallback(error, userContext, methodName)
{
if (error != null) {
alert(error.get_message());
}
userContext.disabled = false;
}
<asp:ListBox runat="server" ID="lbxFriends" onkeydown="deleteListItem(this, event)" DataValueField="friendID" DataTextField="fullname"></asp:ListBox>
[WebMethod]
public static Object insert_lbxFriends(friend f)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("addFriend", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@firstname", f.firstname);
command.Parameters.AddWithValue("@lastname", f.lastname);
return command.ExecuteScalar();
}
}
}
// Basically the class needed for our JSON "action"
public class friend
{
public String firstname { get; set; }
public String lastname { get; set; }
}
function addListItem()
{
// JSON
var friend =
{
firstname: $get("txtfirstname").value,
lastname: $get("txtlastname").value
};
PageMethods.insert_lbxFriends(friend, succeededInsertedCallback, failedInsertedCallback, friend);
}
function succeededInsertedCallback(result, userContext, methodName)
{
var name = userContext.firstname + " " + userContext.lastname;
$get("lbxFriends").options.add(new Option(name, result));
}