Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
![]() |
•
•
Join Date: Aug 2005
Location: Bangalore, India
Posts: 36
Reputation:
Rep Power: 4
Solved Threads: 0
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: Aug 2005
Location: Bangalore, India
Posts: 36
Reputation:
Rep Power: 4
Solved Threads: 0
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:
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.
•
•
Join Date: Jul 2005
Location: Kansas City, Missouri
Posts: 17
Reputation:
Rep Power: 4
Solved Threads: 3
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
Page.RegisterClientScriptBlock()
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
![]() |
Similar Threads
Other Threads in the ASP.NET Forum
- 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
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode