简体   繁体   中英

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, aspx.cs) using scriptregister . but my question is how can I access the JavaScript/jQuery functions from (.cs) files. I'm generating some html tags in (.cs) file therefore in need of to call JavaScript/jQuery function from (.cs) classes.

The files I have are(for example):

  • Default.aspx
  • Default.aspx.cs
  • And Web.cs (My concern is to call JavaScript/jQuery function from web.cs)

Actually, What I'm trying to do is to generate some HTML code in Web.cs (Business Object) and while generating HTML code in web.cs i need some other dynamic HTML code generated by jQuery function which output is my concern: var output ="My HTML Code Generated by jQuery function"

Now I need the above jQuery output to merger with Web.cs HTML code generating.

Kindly let me know how can it be possible to call jQuery function from web.cs and get back the result from jQuery to web.cs in order to merger?

Thanks

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.

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.

This is done using Page.RegisterStartupScript Method

    <%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public void Page_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 = new StringBuilder();
      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>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

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

Unless you have an absolutely rock solid use case, you should reconsider having your JavaScript rendering from c# at all.

Typically, it's much simpler to separate the two. Generate your markup using c#, and "tag" the appropriate elements (using CSS classes probably, since ASP.NET has such awful handling of ids).

Then simply use jQuery to find and manipulate those tagged elements, but do so using an external js script, rather than generating the JavaScript itself inline.

You could pass in the ScriptManager to Web.cs and then use the same methods you use in the code-behind, but you might be better off just constructing and returning the javascript string from Web.cs, and still calling js from your code-behind like you have been doing.

If you simply want to register some client script from a stand alone cs file, the only way I can see this working is to pass your function a reference to the ClientScriptManager for the page.

The script registration actually happens on the "Page" object, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM