c# method within javascript

Posted by Sudhir under JavaScript on 1/14/2010 | Views : 3130 | Status : [Member] | Replies : 4
How to call c# method in Javascript.

Suppose that we make a method in c# and want to call through javascript




Responses

Posted by: Nishithraj on: 1/14/2010 [Member] Bronze

Up
0
Down
Whether you are using ASP.NET...

If Yes, you can't call a server side method directly from client side.

You can use any of these following methods

First mehod is through page methods of update panel as follows

<asp:ScriptManager ID="ScriptManager1" runat="server" 

EnablePageMethods="true" />
<script language="javascript">
function UpdateTime() {
PageMethods.GetCurrentDate(OnSucceeded, OnFailed);
}

function OnSucceeded(result, userContext, methodName) {
$get('Label1').innerHTML = result;
}

function OnFailed(error, userContext, methodName) {
$get('Label1').innerHTML = "An error occured.";
}
</script>
<asp:Label runat="server" ID="Label1" Text="Update Me!" /><br />
<input type="button" id="Button2" value="Web Method Update"
onclick="UpdateTime();" />

[WebMethod]
public static string GetCurrentDate()
{
return DateTime.Now.ToLongDateString();
}


Here the disadvantage is "You can call only a static method".

second method is through ICallbackEventHandler as follows


<script type="text/ecmascript">
function LookUpStock()
{
var lb = document.getElementById("ListBox1");
var product = lb.options[lb.selectedIndex].text;
CallServer(product, "");
}

function ReceiveServerData(rValue)
{
document.getElementById("ResultsSpan").innerHTML = rValue;

}
</script>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ClientCallback : System.Web.UI.Page,
System.Web.UI.ICallbackEventHandler
{
protected System.Collections.Specialized.ListDictionary catalog;
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
String cbReference =
Page.ClientScript.GetCallbackEventReference(this,
"arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);

catalog = new System.Collections.Specialized.ListDictionary();
catalog.Add("monitor", 12);
catalog.Add("laptop", 10);
catalog.Add("keyboard", 23);
catalog.Add("mouse", 17);

ListBox1.DataSource = catalog;
ListBox1.DataTextField = "key";
ListBox1.DataBind();

}

public void RaiseCallbackEvent(String eventArgument)
{
if (catalog[eventArgument] == null)
{
returnValue = "-1";
}
else
{
returnValue = catalog[eventArgument].ToString();
}
}
public String GetCallbackResult()
{
return returnValue;
}
}


Third method is through XmlHttpRequest

Kindly refer the following link for that
http://www.mikesdotnetting.com/Article/40/ASP.NET-and-Ajax-using-XmlHttpRequest 


We can achieve all these three methods are through AJAX

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sudhir, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 1/14/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Use ICallBackEventHandler to do it in a basic way if you dont need to add ScriptManager in your application.

It is very easy to do this, Just follow :
http://www.codeproject.com/KB/aspnet/ICallbackEventHandler.aspx

Cheers.


www.abhisheksur.com

Sudhir, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Puneet20884 on: 1/15/2010 [Member] Bronze

Up
0
Down
Better go for ICallBackEventHandler for 2.0 onwards. Appreciating the demonstration given by 2434 here !! Thanks buddy !!


Also XmlHttpObject is quite handy for it and used these day widely with or without JSON ContentType

Best Regards,
Puneet Sharma - Infosys
Pune, India

Sudhir, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mehulthakkar1 on: 1/18/2010 [Member] Starter

Up
0
Down
Hi

Refer article on dotnetfunda at

http://www.dotnetfunda.com/articles/article757-ajax-dll-.aspx

Thanks
Mehul Thakkar

Sudhir, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response