|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
UserControl Self-Inserting JavaScriptone or two JavaScript functions. I want to assure that those functions will be on the Page once and only once, but I don't want to have to remember to insert the JavaScript on the page itself. Instead, I want the UserControl to basically do this: Each time the UserConrtol starts (PreRender? I'm not sure), I want it to check if a JavaScript clock is loaded into the containing Page. If it is already there, the control does nothing and just continues. If it's *not* there, it inserts the JavaScript block itself. This way, we're sure that the JavaScrip will be there if the control is there, but we don't have to worry about it appearing on the page more than once. I think I should be using the Page.ClientScript.IsClientScriptBlockRegistered() function and also Page.ClientScript.RegisterClientScriptInclude() but I'm not sure how and I'm not sure where in the life-cycle of the UserControl, this stuff can be tested and altered. Any help, or even code-samples are much appreciated! Alex Hello Alex,
I suggest you use the code as below: if (!ClientScript.IsStartupScriptRegistered(this.GetType(), "test")) { ClientScript.RegisterStartupScript(this.GetType(), "test", str.ToString(), true); } It will check if you have registered the script to the page. If not, it will register the script. Please put them in Page_PreRender event. RegisterStartupScript will be render to the area which is below the "form" on the page client. RegisterClientScriptBlock will be render to the area which is above the "form" and "body" on the page client. Sincerely, Vince Xu Microsoft Online Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications. MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 2 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx ================================================== -------------------- Show quoteHide quote | Thread-Topic: UserControl Self-Inserting JavaScript microsoft.public.dotnet.framework.aspnet.webcontrols:4772| thread-index: Acn5ItiOhJevg01dQgCtqHGScO71qQ== | X-WBNR-Posting-Host: 216.205.224.10 | From: =?Utf-8?B?QWxleCBNYWdoZW4=?= <AlexMaghen@newsgroup.nospam> | Subject: UserControl Self-Inserting JavaScript | Date: Mon, 29 Jun 2009 18:34:01 -0700 | Lines: 26 | Message-ID: <EE51F42F-B33F-4F8C-B32E-77D6BAE83***@microsoft.com> | MIME-Version: 1.0 | Content-Type: text/plain; | charset="Utf-8" | Content-Transfer-Encoding: 7bit | X-Newsreader: Microsoft CDO for Windows 2000 | Content-Class: urn:content-classes:message | Importance: normal | Priority: normal | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3168 | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols | Path: TK2MSFTNGHUB02.phx.gbl | Xref: TK2MSFTNGHUB02.phx.gbl Show quoteHide quote | NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149 | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols | | I am building an ASP.NET UserControl. The UserControl will be making use of | one or two JavaScript functions. I want to assure that those functions will | be on the Page once and only once, but I don't want to have to remember to | insert the JavaScript on the page itself. | | Instead, I want the UserControl to basically do this: Each time the | UserConrtol starts (PreRender? I'm not sure), I want it to check if a | JavaScript clock is loaded into the containing Page. If it is already there, | the control does nothing and just continues. If it's *not* there, it inserts | the JavaScript block itself. | | This way, we're sure that the JavaScrip will be there if the control is | there, but we don't have to worry about it appearing on the page more than | once. | | I think I should be using the | Page.ClientScript.IsClientScriptBlockRegistered() | function and also | Page.ClientScript.RegisterClientScriptInclude() | | but I'm not sure how and I'm not sure where in the life-cycle of the | UserControl, this stuff can be tested and altered. | | Any help, or even code-samples are much appreciated! | | Alex |
Show quote
Hide quote
"Vince Xu [MSFT]" <v-vin***@online.microsoft.com> wrote in message I thought RegisterStartupScript performs the IsStartupScriptRegistered check news:I0JFFNT#JHA.2572@TK2MSFTNGHUB02.phx.gbl... > Hello Alex, > > > I suggest you use the code as below: > > if (!ClientScript.IsStartupScriptRegistered(this.GetType(), > "test")) > { > ClientScript.RegisterStartupScript(this.GetType(), "test", > str.ToString(), true); > } > > It will check if you have registered the script to the page. If not, it > will register the script. Please put them in Page_PreRender event. > > RegisterStartupScript will be render to the area which is below the "form" > on the page client. > RegisterClientScriptBlock will be render to the area which is above the > "form" and "body" on the page client. > > Sincerely, > > Vince Xu > > Microsoft Online Support > > so you don't have to call it yourself? Mythran Hello Mythran,
Yes, you're right. As you said, RegisterClientScriptBlock and RegisterStartupScript will do check if the current key has been registered. If the current key is exist in list, it will return the related value instead of setting the new value the user registered. According to the MSDN document, we suggest developer use IsStartupScriptRegistered/IsClientScriptBlockRegistered to check if it has been registered before RegisterClientScriptBlock/RegisterStartupScript. http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupsc ript.aspx Because in this way, we can prevent executing the superfluous code which is in the if block. Regards, Vince Xu Microsoft Online Support -------------------- | From: "Mythran" <Mythran@community.nospam> <I0JFFNT#JHA.2***@TK2MSFTNGHUB02.phx.gbl>| References: <EE51F42F-B33F-4F8C-B32E-77D6BAE83***@microsoft.com> Show quoteHide quote | In-Reply-To: <I0JFFNT#JHA.2***@TK2MSFTNGHUB02.phx.gbl> microsoft.public.dotnet.framework.aspnet.webcontrols:4777| Subject: Re: UserControl Self-Inserting JavaScript | Date: Tue, 30 Jun 2009 08:38:53 -0700 | Lines: 4 | Message-ID: <B0AA2EF2-4A18-46D5-A335-5B8DA57CB***@microsoft.com> | MIME-Version: 1.0 | Content-Type: text/plain; | format=flowed; | charset="Windows-1252"; | reply-type=original | Content-Transfer-Encoding: 7bit | X-Priority: 3 | X-MSMail-Priority: Normal | Importance: Normal | X-Newsreader: Microsoft Windows Live Mail 14.0.8064.206 | X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8064.206 | X-MS-CommunityGroup-PostID: {B0AA2EF2-4A18-46D5-A335-5B8DA57CB682} | X-MS-CommunityGroup-ThreadID: EE51F42F-B33F-4F8C-B32E-77D6BAE83C38 | X-MS-CommunityGroup-ParentID: 70688A68-23CC-4C85-93D5-7DE8354B80F0 | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols | Path: TK2MSFTNGHUB02.phx.gbl | Xref: TK2MSFTNGHUB02.phx.gbl Show quoteHide quote | NNTP-Posting-Host: TK2MSFTNGHUB02.phx.gbl 127.0.0.1 | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols | | | "Vince Xu [MSFT]" <v-vin***@online.microsoft.com> wrote in message | news:I0JFFNT#JHA.2572@TK2MSFTNGHUB02.phx.gbl... | > Hello Alex, | > | > | > I suggest you use the code as below: | > | > if (!ClientScript.IsStartupScriptRegistered(this.GetType(), | > "test")) | > { | > ClientScript.RegisterStartupScript(this.GetType(), "test", | > str.ToString(), true); | > } | > | > It will check if you have registered the script to the page. If not, it | > will register the script. Please put them in Page_PreRender event. | > | > RegisterStartupScript will be render to the area which is below the "form" | > on the page client. | > RegisterClientScriptBlock will be render to the area which is above the | > "form" and "body" on the page client. | > | > Sincerely, | > | > Vince Xu | > | > Microsoft Online Support | > | > | | I thought RegisterStartupScript performs the IsStartupScriptRegistered check | so you don't have to call it yourself? | | Mythran | | | I have two more questions about this:
1. How efficient is IsClientScriptBlockRegistered()? If I have 50 of a control on a page and each instantiation of that control performs this IsClientScriptBlockRegistered(), is this a heavy process? It doesn't require parsing the page each time, right? If it's just a lookup in a collection that's already been built than I imagine it's cheap. I just don't want to do something very heavy 50 times for every single time the page is generated. 2. Is there a way to do this some procedure of checking whether the script is present in the page, but using RegisterClientScriptInclude() instead? It's cleaner for me if I can use an instruction to load the JS from file, but I can't find something like "IsClientScriptIncludeRegistered()". How can I do this? Thanks. Alex Show quoteHide quote "Vince Xu [MSFT]" wrote: > Hello Mythran, > Yes, you're right. As you said, RegisterClientScriptBlock and > RegisterStartupScript will do check if the current key has been registered. > If the current key is exist in list, it will return the related value > instead of setting the new value the user registered. > According to the MSDN document, we suggest developer use > IsStartupScriptRegistered/IsClientScriptBlockRegistered to check if it has > been registered before RegisterClientScriptBlock/RegisterStartupScript. > http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupsc > ript.aspx > > Because in this way, we can prevent executing the superfluous code which is > in the if block. > > > Regards, > > Vince Xu > > Microsoft Online Support > > > -------------------- > | From: "Mythran" <Mythran@community.nospam> > | References: <EE51F42F-B33F-4F8C-B32E-77D6BAE83***@microsoft.com> > <I0JFFNT#JHA.2***@TK2MSFTNGHUB02.phx.gbl> > | In-Reply-To: <I0JFFNT#JHA.2***@TK2MSFTNGHUB02.phx.gbl> > | Subject: Re: UserControl Self-Inserting JavaScript > | Date: Tue, 30 Jun 2009 08:38:53 -0700 > | Lines: 4 > | Message-ID: <B0AA2EF2-4A18-46D5-A335-5B8DA57CB***@microsoft.com> > | MIME-Version: 1.0 > | Content-Type: text/plain; > | format=flowed; > | charset="Windows-1252"; > | reply-type=original > | Content-Transfer-Encoding: 7bit > | X-Priority: 3 > | X-MSMail-Priority: Normal > | Importance: Normal > | X-Newsreader: Microsoft Windows Live Mail 14.0.8064.206 > | X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8064.206 > | X-MS-CommunityGroup-PostID: {B0AA2EF2-4A18-46D5-A335-5B8DA57CB682} > | X-MS-CommunityGroup-ThreadID: EE51F42F-B33F-4F8C-B32E-77D6BAE83C38 > | X-MS-CommunityGroup-ParentID: 70688A68-23CC-4C85-93D5-7DE8354B80F0 > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols > | Path: TK2MSFTNGHUB02.phx.gbl > | Xref: TK2MSFTNGHUB02.phx.gbl > microsoft.public.dotnet.framework.aspnet.webcontrols:4777 > | NNTP-Posting-Host: TK2MSFTNGHUB02.phx.gbl 127.0.0.1 > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols > | > | > | "Vince Xu [MSFT]" <v-vin***@online.microsoft.com> wrote in message > | news:I0JFFNT#JHA.2572@TK2MSFTNGHUB02.phx.gbl... > | > Hello Alex, > | > > | > > | > I suggest you use the code as below: > | > > | > if (!ClientScript.IsStartupScriptRegistered(this.GetType(), > | > "test")) > | > { > | > ClientScript.RegisterStartupScript(this.GetType(), > "test", > | > str.ToString(), true); > | > } > | > > | > It will check if you have registered the script to the page. If not, it > | > will register the script. Please put them in Page_PreRender event. > | > > | > RegisterStartupScript will be render to the area which is below the > "form" > | > on the page client. > | > RegisterClientScriptBlock will be render to the area which is above the > | > "form" and "body" on the page client. > | > > | > Sincerely, > | > > | > Vince Xu > | > > | > Microsoft Online Support > | > > | > > | > | I thought RegisterStartupScript performs the IsStartupScriptRegistered > check > | so you don't have to call it yourself? > | > | Mythran > | > | > | > > Hello Alex,
1. IsClientScriptBlockRegistered will check the collection list that's already been built according to the "key" registered. It's fine to loop 50 times I think if there are not too much items in the collection list. 2. Actually, you can use IsClientScriptIncludeRegistered to check the collection list that's already been built by RegisterClientScriptInclude.-- Page.ClientScript.IsClientScriptIncludeRegistered Sincerely, Vince Xu Microsoft Online Support |
|||||||||||||||||||||||