Calling C# Code from JavaScript
Every once in a while people want to call their server-side code from JavaScript. I ran into this particular difficulty myself to find that many people online (including inside the msdn forums) say that this cannot be done. Well I hope it hasn't led too many people astray as I am going to demonstrate it here. So first thing is first:
- Create a new Website (It doesn't even have to be an AJAX-Enabled Website)
- Open the Default.aspx Page and change it to the following:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
Calling C# from JavaScript
<script type="text/javascript">
function RecieveServerData(arg, context)
{
Message.innerText = arg;
}
<form id="form1" runat="server">
<input type="button" value="Callback"
onclick="CallServer('Calling From Button', null)" />
<span id="Message">
Now for the code-behind:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page,
ICallbackEventHandler
{
string fromClient;
///
/// Fired when the page loads. Creates
/// JavaScript to connect to the server.
///
///
///
protected void Page_Load(
object sender, EventArgs e)
{
// Get javascript function from the
// server that is capible of connecting
// to our server. RecieveServerData is
// the javascript method we will call when
// we are done processing the data
string reference =
this.ClientScript.GetCallbackEventReference(
this, "arg", "RecieveServerData", "");
// Generate the javascript that can connect
// to the server
string callBackScript =
@" function CallServer(arg, context)
{" + reference + ";}";
// Put the javascript on the page
this.ClientScript.RegisterClientScriptBlock(
this.GetType(), "CallServer",
callBackScript, true);
}
///
/// This is the method called by the
/// javascript. Here you can do some server
/// processing to manage what you are going
/// to give back to the client if anything.
///
///
public void RaiseCallbackEvent(
string eventArgument)
{
/* Im saving stuff here */
this.fromClient = eventArgument;
}
///
/// This is the method that passes back
/// to the javascript.
///
///
public string GetCallbackResult()
{
return this.fromClient;
}
}
And that is it. Run it and you will be sending information to the server, processing it, and sending the information back to the client. How does it work? The ICallbackEventHandler interface defines the contract allowing the javascript to connect to it. The registration in the Page_Load() generates javascript capible of connecting to the server, and the rest is your server-side code. This functionality is not limited to pages as well. As long as you can access the page to do the script registration, you can create server-side code that can be connected to by client-side javascript.
This is very useful, but be aware of the security risks involved. Always make sure to validate the input. Hope this helps.
Comments
Hi,
I am using .net 2003. I can't able to use System.Web.UI.WebControls.Web.
So I don't access ClientScript property.
plz help me.