Professional Web Applications Themes

dynamically adding to HEAD tag - ASP.NET General

What is the beat way to dynamically write/add to the HEAD tag of an ASPX page (the <head runat=server ... is too error prone and not very repeatable)? Thanks....

  1. #1

    Default dynamically adding to HEAD tag

    What is the beat way to dynamically write/add to the HEAD
    tag of an ASPX page (the <head runat=server ... is too
    error prone and not very repeatable)?

    Thanks.
    Guest

  2. #2

    Default Re: dynamically adding to HEAD tag

    Hi,
    I'm trying to automate the process of writing <SCRIPT> and <LINK> tags
    in-between the HEAD tags (to guarantee that they are processed first by the
    browser) in all our ASPX pages.
    I need to do this from code-behind code.

    I've read some "solutions" such as:
    - Making the existing <HEAD> into a HtmlGenericControl by applying a
    runat=server
    - Adding a PlaceHolder control between the <HEAD> tags.
    But these are not suitable for large-scale web apps with many Pages.

    I want to extend the Page class, since this would also allow WebControls to
    write to the <HEAD> tag, and implement a function similar to the
    Page.RegisterClientScriptBlock().

    Basically, my question is "How do I write in-between the HEAD tags of the
    ASPX page from code-behind code?".


    Example:

    [in WebForm1.aspx.cs]

    public class MyPage : Page
    {

    protected override void OnLoad(EventArgs e)
    {
    // Is there any built-in method to write in-between the <HEAD> tags?
    }

    ....
    }


    Thanks.

    "Jacob Yang [MSFT]" <microsoft.com> wrote in message
    news:phx.gbl... 
    rights. 


    developer Guest

  3. #3

    Default Re: dynamically adding to HEAD tag

    Hi,

    Based on my research and experience, it seems that it is impossible to
    insert client-side script between the <Head> tags with ASP.NET. please
    refer to the following URL:

    http://www.fawcette.com/vsm/2002_07/magazine/columns/aspnet/default_pf.asp

    It states that:

    "It might strike you as odd that ASP.NET inserts client <script> blocks
    inside the HTML form instead of within the <head> section that most people
    choose for scripts. Microsoft's designers needed to tie the rendering of
    the script blocks to a server-side control inside the page, and not to
    something that's only literal text. Microsoft chose to put the code inside
    the <form> tag rather than make you insert <head runat=server> in your
    pages. This is based on the valid assumption that you need a server-side
    form if you're using controls. "

    Best regards,

    Jacob Yang
    Microsoft Online Partner Support
    <MCSD>
    Get Secure! 每 www.microsoft.com/security
    This posting is provided ※as is§ with no warranties and confers no rights.

    Jacob Guest

  4. #4

    Default Re: dynamically adding to HEAD tag

    Jacob, that's completely unrelated to this thread. The article you're
    quoting simply states that server controls will insert their scripts next to
    their HTML instead of in the head element - that makes sense as each control
    generates a continuous stream of characters instead of splitting the result
    into two pieces and trying to insert one somewhere else in the page. It does
    not say that it's impossible to create dynamic HTML inside the head.

    Jerry

    "Jacob Yang [MSFT]" <microsoft.com> wrote in message
    news:phx.gbl... 
    rights. 


    Jerry Guest

  5. #5

    Default Re: dynamically adding to HEAD tag

    Hi,
    there are ways, but none are "clean" solutions.
    I think the following method is problably the best I've found (w/o having to
    implement my own Page factory):



    [in WebForm1.aspx.cs]

    public class MyPage : Page
    {
    ....

    protected override void Render(HtmlTextWriter writer)
    {
    WriteToHeaderTag("<script>function global(){...</script>");

    base.Render (writer);
    }


    private void WriteToHeaderTag(string tag)
    {
    LiteralControl headCtl = null;

    foreach(Control ctl in this.Controls)
    {
    if( ctl is LiteralControl)
    {
    headCtl = (LiteralControl) ctl;

    // TODO: handle non existing </HEAD> tag

    // write to the head
    if (headCtl.Text.ToUpper().IndexOf("</HEAD>") > -1)
    {
    headCtl.Text = InsertTag(headCtl.Text, tag, "</HEAD>");
    break;
    }

    }
    }
    }


    private string InsertTag(string stringSrc, string stringToAdd, string
    stringMarker)
    {
    return Regex.Replace(stringSrc, stringMarker , stringToAdd +
    stringMarker);
    }



    anyone has improvements OR a better solution?

    Thanks.

    "Jacob Yang [MSFT]" <microsoft.com> wrote in message
    news:phx.gbl... 
    rights. 


    developer Guest

  6. #6

    Default Re: dynamically adding to HEAD tag

    I posted one, why in the hell just don't you use server tags inside <head>?
    I personally use repeater in there to insert a list of style sheets, what's
    so difficult about it? There's absolutely no difference between using server
    tags in the body and in the head elements...

    Jerry

    "developer" <com> wrote in message
    news:phx.gbl... 
    to [/ref]
    http://www.fawcette.com/vsm/2002_07/magazine/columns/aspnet/default_pf.asp [/ref]
    people [/ref]
    inside 
    > rights. 
    >
    >[/ref]


    Jerry Guest

  7. #7

    Default Re: dynamically adding to HEAD tag

    Hi,

    we're developing hundreds of pages among multiple developers.

    I don't want to maintain theses Pages individually.

    I need to cater for the fact that this Control could accidentally be removed
    or is misplaced somewhere else OTHER than in the HEAD section.

    Each developer must develop his Pages w/o having to remember to add a
    Control to the Head.

    Templates are only good if everyone uses them all the time AND no one mucks
    with the Page HTML (which will happen).

    Thanks.

    "Jerry III" <com> wrote in message
    news:%phx.gbl... 
    <head>? 
    what's 
    server [/ref]
    having [/ref][/ref]
    to [/ref]
    > http://www.fawcette.com/vsm/2002_07/magazine/columns/aspnet/default_pf.asp [/ref][/ref]
    blocks [/ref]
    > people [/ref][/ref]
    of [/ref]
    > inside [/ref][/ref]
    server-side 
    > > rights. 
    > >
    > >[/ref]
    >
    >[/ref]


    developer Guest

  8. #8

    Default Re: dynamically adding to HEAD tag

    Hi Jerry,

    I am sorry if there is any misunerstanding. I do not mean it is impossible
    to implement the DHTML in <head></head> section. As we have mentioned
    before, with the
    placeholder control, we can implement what we require in this thread.

    The article means that with the build-in asp.net method, we cannot directly
    insert any stuff into the section. instead, we should resort to other ways,
    for example, using a placeholder as a container...

    Best regards,

    Jacob Yang
    Microsoft Online Partner Support
    <MCSD>
    Get Secure! 每 www.microsoft.com/security
    This posting is provided ※as is§ with no warranties and confers no rights.

    Jacob Guest

Similar Threads

  1. Dynamically Adding Controls
    By Nathan Sokalski in forum ASP.NET Building Controls
    Replies: 9
    Last Post: November 25th, 03:21 PM
  2. Dynamically Adding DataLists
    By Nathan Sokalski in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: November 8th, 05:51 AM
  3. Dynamically adding DSN?
    By Phyte@Omegabyte in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: July 6th, 12:03 PM
  4. Dynamically adding a row into an gridview
    By Henk in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: September 22nd, 09:21 PM
  5. adding buttons dynamically
    By evya in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 19th, 08:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139