Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Thursday, December 6, 2007

C# ASP.NET using JavaScript

For ASP.NET, use the ClientScriptManager to register client scripts.

For example, in the Page_Load code behind I'll use:


ClientScript.RegisterClientScriptBlock(typeof(string), "MyScriptShow", "<script language=javascript>function showIt() { alert('showed it'); }</script>");

If the javascript was in a js file register it in the code behind with:

ClientScript.RegisterClientScriptBlock(typeof(string), "myScripts", "<script language='javascript' src='scripts/myScripts.js'></script>");

In the ASPX page I'll use;

<asp:Button ID="Button1" runat="server" OnClientClick="showIt()" Text="Button" />
Resource links: link, link

Note: Use the ClientScript.RegisterClientScriptBlock or ClientScript.Register_x methods (since Page.Register_x methods are version 1.1)

No comments: