Skip to content Skip to sidebar Skip to footer

How To Call Javascript/jquery Function From .cs File C#

I'm working on asp.net (C#) project which include some page files (aspx, aspx.cs) and some only (.cs) file. I'm able to access JavaScript/jQuery functions from page files (aspx, as

Solution 1:

This question doesn't make alot of sense honestly. You may have a misconception about how this all works together.

The c# code is running on the web server, and the output is Html code that is rendered down to the browser, including javascript.

The Javascript is loaded into the browser and executed locally on the client's computer.

If your trying to manipulate or control some aspect of the client page load with javascript or something either add the script to the page itself (.aspx file) register and stream it with Client.Register api calls, or add a < script .... > tag to import it into the file.

I have no idea what Web.cs class is doing, I would assume its some kind of logic or business object. You'll need to communicate the need for the client script from it back to the presentation layer to get it to run client side.

If you are somehow under the impression that you can run javascript from the server side, you are mistaken.

Solution 2:

The ASP code is server-side. The javascript is on the client side.

You can't really call the javascript. You could create an html file via the ASP code that will generate an onload javascript method. Then when the html is loaded, the javascript will run.

Solution 3:

This is done using Page.RegisterStartupScript Method

    <%@ Page Language="C#"%>
<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><scriptrunat="server">
  public voidPage_Load(Object sender, EventArgs e)
  {
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";

    if (!IsClientScriptBlockRegistered(csname1))
    {
        String cstext1 = "<script type=\"text/javascript\">" +
            "alert('Hello World');</" + "script>";
        RegisterStartupScript(csname1, cstext1);
    }

    if (!IsClientScriptBlockRegistered(csname2))
    {
      StringBuilder cstext2 = newStringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      RegisterClientScriptBlock(csname2, cstext2.ToString());
    }
  }
</script><html  ><head><title>RegisterClientScriptBlock Example</title></head><body><formid="Form1"runat="server"><inputtype="text"id="Message" /><inputtype="button"value="ClickMe"onclick="DoClick()" /></form></body></html>

Solution 4:

The script registration actually happens on the "Page" object, i.e. Web.cs does not have access to the page to register a script. For example, if you want Web.cs to register a script on Default.aspx page, you'd need to give access to the Default.aspx/cs "Page" object's Page.ClientScript.RegisterClientScriptBlock method through an interface or class definition.

Solution 5:

JavaScript is client side and c# is server side so you can't call JavaScript from server side, but you can schedule a JavaScript to be executed when the page is rendered on the client side...

http://msdn.microsoft.com/en-us/library/aa478975.aspx

Post a Comment for "How To Call Javascript/jquery Function From .cs File C#"