How can we include a clientside script like Java script

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 36
Reputation: aripaka is an unknown quantity at this point 
Solved Threads: 0
aripaka's Avatar
aripaka aripaka is offline Offline
Light Poster

How can we include a clientside script like Java script

 
0
  #1
Aug 30th, 2005
Hi,
I am a novice in .Net. Can anyone please help me by clarifying the below doubts...

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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: How can we include a clientside script like Java script

 
0
  #2
Aug 30th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: aripaka is an unknown quantity at this point 
Solved Threads: 0
aripaka's Avatar
aripaka aripaka is offline Offline
Light Poster

Re: How can we include a clientside script like Java script

 
0
  #3
Aug 31st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: How can we include a clientside script like Java script

 
0
  #4
Aug 31st, 2005
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:

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 17
Reputation: cambia is an unknown quantity at this point 
Solved Threads: 3
cambia's Avatar
cambia cambia is offline Offline
Newbie Poster

Re: How can we include a clientside script like Java script

 
0
  #5
Sep 7th, 2005
Hi aripaka,

To include a client-side script block from your asp.net code behind look into
  1. 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.

  1. public static void RegisterRandomInt(Page page)
  2. {
  3. if (page.IsClientScriptBlockRegistered("randomInt"))
  4. return;
  5.  
  6. System.IO.StringWriter sw = new System.IO.StringWriter();
  7. HtmlTextWriter writer = new HtmlTextWriter(sw);
  8. writer.WriteLine();
  9. writer.WriteBeginTag("script");
  10. writer.WriteAttribute("language", "Javascript");
  11. writer.Write(">");
  12. writer.WriteLine();
  13. writer.WriteLine("<!--");
  14. writer.WriteLine("function randomInt(lowInt, highInt)");
  15. writer.WriteLine("{");
  16. writer.WriteLine(" return Math.floor(Math.random() * Math.abs((highInt+1)-lowInt)) + lowInt;");
  17. writer.WriteLine(" }");
  18. writer.WriteLine("//-->");
  19. writer.WriteEndTag("script");
  20. string script = sw.ToString();
  21.  
  22. page.RegisterClientScriptBlock("randomInt", script);
  23. }

Cheers,
Steve
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC