You can not do this directly. In standard WebForms JavaScript is interpreted by browser and C# by server. What you can do to call a method from server using JavaScript is.
- Use [`WebMethod`][1] as [`attribute`][2] in target methods.
- Add [`ScriptManager`][3] setting [`EnablePageMethods`][4] as `true`.
- Add JavaScript code to call the methods through the object `PageMethods`.
Like this:
Step 1
------
<!-- language: c# -->
public partial class Products : System.Web.UI.Page
{
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static List<Product> GetProducts(int cateogryID)
{
// Put your logic here to get the Product list
}
Step 2: Adding a [`ScriptManager`][3] on the [`Page`][5]
--------------------------------------------
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Step 3: Calling the method using JavaScript
-----------------------------------------------
<!-- language: lang-js -->
function GetProductsByCategoryID(categoryID)
{
PageMethods.GetProducts(categoryID, OnGetProductsComplete);
}
[Take a look at this link.][6]
To call a JavaScript function from server you can use [`RegisterStartupScript`][7]:
<!-- language: c# -->
ClientScript.RegisterStartupScript(GetType(),"id","callMyJSFunction()",true);
[1]:
[To see links please register here]
[2]:
[To see links please register here]
[3]:
[To see links please register here]
[4]:
[To see links please register here]
[5]:
[To see links please register here]
[6]:
[To see links please register here]
[7]:
[To see links please register here]