| | |
How can we include a clientside script like Java script
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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.
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.
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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:
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.
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:
ASP.NET Syntax (Toggle Plain Text)
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
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.
Cheers,
Steve
To include a client-side script block from your asp.net code behind look into
ASP.NET Syntax (Toggle Plain Text)
Page.RegisterClientScriptBlock()
The following is an example C# method which registers a clientside javascript function to generate random numbers.
ASP.NET Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- How to Embed CheckList Box on HTML Page using Java Script (HTML and CSS)
- A question on java script?(Please answer ASAP) (Java)
- java script for main menu (ASP)
- Problems with Windows Script Encoder & #include directive (ASP)
Other Threads in the ASP.NET Forum
- Previous Thread: create an app drag & drop controls in asp.net
- Next Thread: transfer page
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# c#gridviewcolumn cac checkbox click commonfunctions confirmationcodegeneration content courier css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dialog dropdownlist dynamically edit expose fileuploader fill flash formatdecimal forms formview gridview gudi homeedition iframe iis javascript jquery listbox login microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers






