RSS Forums RSS
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 4705 | Replies: 4 | Thread Tools  Display Modes
Reply
Join Date: Aug 2005
Location: Bangalore, India
Posts: 36
Reputation: aripaka is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aripaka's Avatar
aripaka aripaka is offline Offline
Light Poster

How can we include a clientside script like Java script

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

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

  #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  
Join Date: Aug 2005
Location: Bangalore, India
Posts: 36
Reputation: aripaka is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aripaka's Avatar
aripaka aripaka is offline Offline
Light Poster

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

  #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  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

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

  #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:

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  
Join Date: Jul 2005
Location: Kansas City, Missouri
Posts: 17
Reputation: cambia is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
cambia's Avatar
cambia cambia is offline Offline
Newbie Poster

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

  #5  
Sep 7th, 2005
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:50 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC