Hi Guys,

Is it possible to call the C# function from Javasript......

Sushil Kumar :rolleyes:

Dani AI

Generated

You cannot reach into the server process from JavaScript, but you can expose a server endpoint and call it from the browser. In classic ASP.NET Web Forms, the lightest-weight option is a Page Method: a static method on your page marked with [WebMethod] and enabled via ScriptManager. The framework generates a PageMethods proxy you can call from JavaScript without a full postback.

Example (Web Forms):

<!-- .aspx -->
<asp:ScriptManager runat="server" EnablePageMethods="true" />

<script>
  function greet() {
    PageMethods.Greet("Sushil",
      function (result) { console.log(result); },
      function (err) { console.error(err.get_message()); });
  }
</script>
// .aspx.cs
using System.Web.Services;

[WebMethod]
public static string Greet(string name)
{
    return "Hi " + name;
}

Notes:

  • Page methods must be public static and cannot access instance controls; use HttpContext.Current if you need per-request info.
  • Handle errors in the failure callback, and secure sensitive methods (auth, validation, anti-forgery where appropriate).

If you are building new endpoints or not using Web Forms, expose a JSON endpoint (e.g., ASP.NET Web API) and call it with fetch/XHR. See ScriptManager.EnablePageMethods and WebMethodAttribute. For API-based approaches, start with ASP.NET Web API overview.

Recommended Answers

All 13 Replies

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.

Hi Guys,

Is it possible to call the C# function from Javasript......

Sushil Kumar :rolleyes:

Yes it is possible to call C# function through javascript using Ajax extensions in Asp.Net

You're a little late serk.. :)

Aug 24th, 2005

Just a quick thought here. Vfp programmer learning C#, .net, etc. Even with the time differences on the post I found it really helpfull on my learning curve. Thanks guys!!!
Oct 27, 3009!

Just a quick thought here. Vfp programmer learning C#, .net, etc. Even with the time differences on the post I found it really helpfull on my learning curve. Thanks guys!!!
Oct 27, 3009!

Attention Dani ! We have got an alien from future in this board.

my ID :

without using javascript how to call a server side in function in textbox onblur event

ex:

public void disp()
{
Response.Write("Hi");
}

page_Load()
{
TextBox1.Attributes.Add("onblur","disp()");
}

its not working

how to call this function

without using javascript or any other scripts

similarly i need concept from textbox blur event , not any button events

plz help me to get a code ..

Thanking You

You should use web service or wcf (axaj)
send method to web service than shuld run to "this.GetType().InvokeMember" for the method

i am using , very good tecnology same windows application,

JavaScript can not talk with serverside methods since the client is on the Earth and the server is the Moon. You need to use a spaceship to get there. So you need to either postback the page or look into using Ajax.

visit

source >> http://forums.asp.net/t/1184597.aspx

There is a way to do that. In your page add a button and then call button click via javascript. The event on button click will call a C# method after OnClick is called.

There is a sample for you:

<script language="javascript" type="text/javascript">

function ParentWindowFunction()

{
    document.getElementById('<%=cmdUpdateField.ClientID%>').click();
}

</script>


//---------------
<asp:LinkButton CssClass="dnnPrimaryAction"
ID="cmdUpdateField" runat="server" Text="Update Fields" 
onclick="cmdUpdateField_Click" ></asp:LinkButton>


//--------------In your code behind page
protected void cmdUpdateField_Click(object sender, EventArgs e)
{
    DoSomething();
}

As you can see. When you call javascript name ParentWindowFunction it will active the method at code behind. This is how javascript communicate with C#

bump

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.