German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Add JavaScript to asp:Content block dynamically in ASP .NET

Written by kamal on Apr 30, 2007
How to Insert JavaScript to the page when you are using a Master Page?

Explanation:

With the introduction of ASP .NET 2.0 many programers are having problem with insertion of Javascript to the page.

We will discribe here a short over view of new way to insert a dynamic Javascript in to your page. 
This short article is kind of extension to a detailed KB at MSDN -
Using JavaScript Along with ASP.NET 2.0. Here we are discribing a new factor which rises with the unique Id of a web control.


In this example we are copying Text from a TextBox to the Client Clipboard.

Here is our TextBox and a Button Control.

<input id="TextBox1" runat="server" type="text" />
<Button ID="Button1" runat="server">CopyButton> 


These both Html Web Controls are embedded in to asp:Content i.e We are using Master Page. Now we are going to Add/Insert the JavaScript at Page_Load event of our Page.

 Button1.Attributes.Add("onclick", @"CopiedTxt = document.getElementById('" 
+ TextBox1.UniqueID +
@"').value; window.clipboardData.setData('text',CopiedTxt);"
);

TextBox1.UniqueID will get the Dynamically created ID for the Control.

One other way would be like this (new in ASP .NET 2.0):

Page.ClientScript.RegisterStartupScript(this.GetType(),"myjavascript1" ,
@"function PasteIt(){

CopiedTxt = document.getElementById('" + TextBox1.UniqueID + @"').value; window.clipboardData.setData('text',CopiedTxt);
}"
,true);

ClientScript is a new Object introduce in ASP .NET 2.0. Before ASP.NET 2.0, you would have needed to employ the RegisterStartupScript and the RegisterClientScriptBlock methods. These methods are now considered obsolete. Both of these possibilities for registering scripts in ASP.NET 1.x required a key/script set of parameters. Because two separate methods were involved, there was an extreme possibility that some key name collisions would occur. The Page.ClientScript property is meant to bring all the script registrations under one umbrella, making your code less error prone.

 

Visitors/Readers Comments
(for questions please use The Forum)



Asif Nadem

very nice article

19/06/2007 01:22:15 UTC

nickle
I would also like to appritiate this nice Article. Kamal you are saviour.

19/06/2007 12:28:53 UTC

Name

very nice article

I would also like to appritiate this nice Article. Kamal you are saviour.

 

03/07/2007 11:56:51 UTC

Chandana
Good Article...  Bingo!!

30/07/2007 07:02:36 UTC

Duke
not much use bro ...

31/08/2007 04:43:26 UTC

saleem
Nice Once.

17/10/2007 22:33:27 UTC

kannan

I've method for comparing two textboxes' values..have written a javascript  code which checks for the larger value. As u explained above, its possible from the code behind..

i want to call a javascript on textbox' "OnBlur" method, which passes id of two textboxes....but its not taking the ids ..plz check the code given below

  

<asp:TextBox ID="txtPrevMilAV" onblur="javascript:return MileageValidation(this, '<%= txtCurMilAV.ClientID %>');">asp:TextBox>

 

 

18/10/2007 04:27:41 UTC

Kamal

Can you also Past your JavaScript function MileageValidation()?

try this way to call the funtion:

<asp:TextBox ID="txtPrevMilAV" onblur="MileageValidation(this, '');">asp:TextBox>

if above function is correctly coded then it must work!

18/10/2007 09:04:36 UTC

Hamid Shahid
Thansk Kamal
This is great, but I don't understand how would anyone work with complex javascript methods using this method. It's like putting all the javascript method inside a string :(

23/12/2007 17:02:30 UTC

puru

hi this is puru. ur article is userful to me

thanks

17/01/2008 02:48:04 UTC

Dharmeen

hi i am dharmeen , this article helped me lot .

Thanks

15/04/2008 08:47:04 UTC

rachit
fine..

25/07/2008 13:56:43 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)