ASP.NET Reference Body element server side

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShadowLocke
    New Member
    • Jan 2008
    • 116

    ASP.NET Reference Body element server side

    How can I reference the body element from server side code if the body does not have an id?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by ShadowLocke
    How can I reference the body element from server side code if the body does not have an id?
    Why do you want to reference the body element?
    Put a runat="server" in the <body> element to be able to access it from your server side code....normall y you don't need to access the body element though...

    -Frinny

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Originally posted by Frinavale
      Why do you want to reference the body element?
      Put a runat="server" in the <body> element to be able to access it from your server side code....normall y you don't need to access the body element though...

      -Frinny
      But if it doesn't have an ID, you still can't access it.

      Comment

      • ShadowLocke
        New Member
        • Jan 2008
        • 116

        #4
        Originally posted by Frinavale
        Why do you want to reference the body element?
        Put a runat="server" in the <body> element to be able to access it from your server side code....normall y you don't need to access the body element though...

        -Frinny
        Ive created a web control that that needs to add and event listener for onkeydown to the body. What ive done for now is during the control rendering, just wrote the javascript out to the page that will do it from there. But i think it would be a much cleaner solution if i could access the body from the web control.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Well, give the body an id, and a runat="server" attribute.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by ShadowLocke
            Ive created a web control that that needs to add and event listener for onkeydown to the body. What ive done for now is during the control rendering, just wrote the javascript out to the page that will do it from there. But i think it would be a much cleaner solution if i could access the body from the web control.
            I did the same thing and went through the same thought process as you...I originally tried accessing the <body> tag to do this... but I discovered that you don't need to use the <body> tag at all. You just attach an event to the window...

            eg (JavaScript code of course):

            [code=javascript]
            If(window.attac hEvent){
            window.attachEv ent("onload","t heFunctionThatI sCalledDuringOn Load()");
            }else
            {
            window.addEvent Listener("load" ,"theFunctionTh atIsCalledDurin gOnLoad()");
            }
            [/code]

            -Frinny

            Comment

            Working...