calling C# method from javascript

Asked By mostafa hamdy
29-Nov-06 05:49 AM
Earn up to 0 extra points for answering this tough question.

Hi all

please I want to call C# method from javascript code in my web page please how can I do som thing like that please if any body can help me send me

regards

Mostafa

  re:calling C# method from javascript

K Pravin Kumar Reddy replied to mostafa hamdy
29-Nov-06 05:58 AM
Not directly, no. Server-side functions live server-side, client-side functions live on the client.

In fact, the only way to get ANY server-side code to run, is to submit the form. ASP.NET unrolls the HTTP headers, unpackages the hidden ViewState variable, re-instantiates objects, and then decides what events to raise based on all of the above.

What you can do is have your JavaScript function set a hidden form variable, then do a form.submit().

Then, in your Page_Load handler on the server, use the Request object to check for the presence and/or value of this variable, and call the appropriate server-side method.

  There are several ways to do this

Peter Bromberg replied to mostafa hamdy
29-Nov-06 07:09 AM

besides "submitting the form".

1) You can use XmlHttp or the built in ClientCallback infrastructure to actually make a GET call to the page and a method call based on parameters on the QueryString, and marshal the results back into the client-side DOM via the ResponseText property.

2) You can use an infrastructure that is specifically designed to do this - such as Anthem.Net, MagicAjax, or Microsoft ASP.NET AJAX (formerly "ATLAS").

Of the 3 mentioned, right now I'd recommand Anthem.Net as it is easy to learn, lightweight, and highly extensible.

 

  Toolbar user control

mostafa hamdy replied to Peter Bromberg
29-Nov-06 07:33 AM

Hi all

I have tool bar user control which contains 4 buttons ,and I add for each property to enable and disable this button, when I use it in web page it works good but when I try to use the enable/disable property of the user control in some method  or event of the web page it doesn't work at all but it works only in the page_load event and no method else

please if any body have face some problem like this with user contol or know solution for this problem please send me

best regards

Mostafa

  pls try this
sundar k replied to mostafa hamdy
29-Nov-06 07:55 AM

in javascript lets say you have a method called "hi" which expects a parameter.

<script>

function hi(var someVar)

{

   document.write("hi " + someVar);

}

</script>

in .NET on a method you wish to call the javascript method from:

string whatever = "blahblah";

Page.RegisterStartupScript("myScript", "<script language=JavaScript>hi('" + whatever + "');</script>");

I hope that is what you are looking for and hope it helps!

 

Create New Account