UserControl Self-Inserting JavaScript

Discussion in 'ASP .Net Web Controls' started by Alex Maghen, Jun 30, 2009.

  1. Alex Maghen

    Alex Maghen Guest

    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

    -----------------------------------------------------------------------------
    Less Spam Better enjoyable experience
    Visit : news://spacesst.com
    Alex Maghen, Jun 30, 2009
    #1
    1. Advertising

  2. 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
    ==================================================

    --------------------
    | Thread-Topic: UserControl Self-Inserting JavaScript
    | thread-index: Acn5ItiOhJevg01dQgCtqHGScO71qQ==
    | X-WBNR-Posting-Host: 216.205.224.10
    | From: =?Utf-8?B?QWxleCBNYWdoZW4=?= <>
    | Subject: UserControl Self-Inserting JavaScript
    | Date: Mon, 29 Jun 2009 18:34:01 -0700
    | Lines: 26
    | Message-ID: <>
    | 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
    microsoft.public.dotnet.framework.aspnet.webcontrols:4772
    | 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
    |


    -----------------------------------------------------------------------------
    Less Spam Better enjoyable experience
    Visit : news://spacesst.com
    Vince Xu [MSFT], Jun 30, 2009
    #2
    1. Advertising

  3. Alex Maghen

    Mythran Guest

    "Vince Xu [MSFT]" <> wrote in message
    news:I0JFFNT#...
    > 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
    Mythran, Jun 30, 2009
    #3
  4. 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" <>
    | References: <>
    <I0JFFNT#>
    | In-Reply-To: <I0JFFNT#>
    | Subject: Re: UserControl Self-Inserting JavaScript
    | Date: Tue, 30 Jun 2009 08:38:53 -0700
    | Lines: 4
    | Message-ID: <>
    | 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]" <> wrote in message
    | news:I0JFFNT#...
    | > 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
    |
    |
    |
    Vince Xu [MSFT], Jul 1, 2009
    #4
  5. Alex Maghen

    Alex Maghen Guest

    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

    "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" <>
    > | References: <>
    > <I0JFFNT#>
    > | In-Reply-To: <I0JFFNT#>
    > | Subject: Re: UserControl Self-Inserting JavaScript
    > | Date: Tue, 30 Jun 2009 08:38:53 -0700
    > | Lines: 4
    > | Message-ID: <>
    > | 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]" <> wrote in message
    > | news:I0JFFNT#...
    > | > 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
    > |
    > |
    > |
    >
    >
    Alex Maghen, Jul 3, 2009
    #5
  6. 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
    Vince Xu [MSFT], Jul 6, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    578
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    592
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    399
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    372
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    401
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page