Hi,
I am a novice in .Net. Can anyone please help me by clarifying the below doubts... :cool:

How can we include a client side script like Javascript or VBscript in an ASP.Net page?

Can we have multiple <script> blocks in ASP.Net page?

Dani AI

Generated

Short answer up front: a plain <script> block (no runat="server") is sent to the browser and behaves exactly like script in any HTML page — you can have many script blocks or reference external .js files. For modern pages prefer external files and non-blocking loading (defer/async) and avoid VBScript (it’s essentially IE-only and Microsoft is phasing it out). (learn.microsoft.com)

If you need to emit client script from code-behind, use the page’s client-script APIs rather than string-concatenating HTML into the response. The ClientScriptManager methods (RegisterClientScriptBlock / RegisterStartupScript) let you add functions or startup calls with a unique key so the same script isn’t emitted twice; RegisterStartupScript is intended for code that should run after the page is rendered. If the page uses ASP.NET AJAX UpdatePanel partial postbacks, register scripts with ScriptManager methods so they run correctly after async updates. (learn.microsoft.com)

A tiny, concrete pattern (C#) for a simple startup call from code-behind:

Page.ClientScript.RegisterStartupScript(
    GetType(), "pageInit", "initPage();", true);

The last boolean tells ASP.NET to add the <script> tags for you. Use meaningful keys and check registration methods if you need idempotence. (learn.microsoft.com)

Practical tips and gotchas pulled from the thread: is right that server controls render HTML — use Attributes.Add or control OnClientClick to attach handlers from server code when you just need one attribute. ’s suggestion to register reusable script blocks from code-behind is useful; just prefer external .js files for maintenance. If client-side code must reference server controls, use ClientID or set ClientIDMode (Static/Predictable) so IDs are stable for your JavaScript. For UpdatePanel scenarios, re-run init logic after partial postbacks (or hook Sys.WebForms.PageRequestManager endRequest). (learn.microsoft.com)

If anything here needs deeper examples for a specific scenario (UpdatePanel, MasterPages, user controls), show that markup/code and a focused snippet will be provided.

Recommended Answers

All 5 Replies

ASP.NET produces HTML. The ASP.NET objects each "render" an HTML element, specific to the browser/OS/version. So whatever rules apply about mixing and embedding script with HTML applies as well.

The PAGE object has the "AddStartupScript" method, which doesn't do what you think it might (code the onload event of the body), but rather adds a script body to the end of the page.

Hi tgreer,

Thanx for the reply. What if i include some javascript in another script block without runat="server" attribute? Does that work as cliebtside script?

Yes, that works. You can also attach client-side event handlers with the "Add" method of an ASP.NET Server Object's "Attributes" collection.

So if you have a JavaScript function which you've placed in your document's "HEAD" section, named "myFunction()", and you want it to run when "myButton" is clicked, you can code:

myButton.Attributes.Add("onclick","<script>return myFunction()</script");

An important note: the "myFunction" JavaScript function should properly return "true" or "false". If "true", then the form submit occurs, and then the server-side "click" event handler, if any, will run. If "false", then the form submit is cancelled.

Hi aripaka,

To include a client-side script block from your asp.net code behind look into

Page.RegisterClientScriptBlock()

This is very useful for scripts you expect to use in many aspx pages.

The following is an example C# method which registers a clientside javascript function to generate random numbers.

public static void RegisterRandomInt(Page page)
{
	if (page.IsClientScriptBlockRegistered("randomInt"))
		return;

	System.IO.StringWriter sw = new System.IO.StringWriter();
	HtmlTextWriter writer = new HtmlTextWriter(sw);
	writer.WriteLine();
	writer.WriteBeginTag("script");
	writer.WriteAttribute("language", "Javascript");
	writer.Write(">");
	writer.WriteLine();
	writer.WriteLine("<!--");
	writer.WriteLine("function randomInt(lowInt, highInt)");
	writer.WriteLine("{");
	writer.WriteLine("   return Math.floor(Math.random() * Math.abs((highInt+1)-lowInt)) + lowInt;");
	writer.WriteLine("  }");
	writer.WriteLine("//-->");
	writer.WriteEndTag("script");
	string script = sw.ToString();

	page.RegisterClientScriptBlock("randomInt", script);
}

Cheers,
Steve

Here I have found another example to add client script events to asp.net using Attributes.Add

just have a look on this example visit

Thanks

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.