Take the 2-minute tour ×

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?


share|improve this question
add comment

8 Answers

up vote 0 down vote accepted

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.

share|improve this answer
i have modified my question, plz have a look of it. –  irfan Jul 21 '11 at 6:04
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

This is done using Page.RegisterStartupScript Method

    <%@ Page Language="C#"%>
<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.'} </");
      RegisterClientScriptBlock(csname2, cstext2.ToString());
<html  >
    <title>RegisterClientScriptBlock Example</title>
     <form id="Form1"
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
share|improve this answer
add comment

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...


share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


