Changing Body attribute

Discussion in 'ASP .Net' started by tshad, Dec 13, 2004.

  1. tshad

    tshad Guest

    Is there a way during Page_Load to change or add an attribute to the Body
    tag?

    I want to be able to change the onLoad body attribute to do a focus on one
    of my text boxes, such as:

    onLoad="document.forms[0].txtLogon.focus();"

    The problem is I have my <body> in an include file and want to set the
    onLoad attribute during Page_Load time.

    Thanks,

    Tom.
     
    tshad, Dec 13, 2004
    #1
    1. Advertising

  2. tshad

    bruce barker Guest

    just use javascript:

    <script>document.body.onload = function()
    {document.forms[0].txtLogon.focus();};</script>

    -- bruce (sqlwork.com)


    "tshad" <> wrote in message
    news:...
    | Is there a way during Page_Load to change or add an attribute to the Body
    | tag?
    |
    | I want to be able to change the onLoad body attribute to do a focus on one
    | of my text boxes, such as:
    |
    | onLoad="document.forms[0].txtLogon.focus();"
    |
    | The problem is I have my <body> in an include file and want to set the
    | onLoad attribute during Page_Load time.
    |
    | Thanks,
    |
    | Tom.
    |
    |
     
    bruce barker, Dec 13, 2004
    #2
    1. Advertising

  3. tshad

    Karl Seguin Guest

    There are ways, but why not just use Page.RegisterStartupScript?

    Dim str As New System.Text.StringBuilder
    str.Append("<script language=""JavaScript"">")
    str.Append(System.Environment.NewLine)
    str.Append("document.forms[0].txtLogon.focus();")
    str.Append(System.Environment.NewLine)
    str.Append("</script>")
    Page.RegisterStartupScript("SetFocus", str.ToString())

    Nice function to have in a utility class...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "tshad" <> wrote in message
    news:...
    > Is there a way during Page_Load to change or add an attribute to the Body
    > tag?
    >
    > I want to be able to change the onLoad body attribute to do a focus on one
    > of my text boxes, such as:
    >
    > onLoad="document.forms[0].txtLogon.focus();"
    >
    > The problem is I have my <body> in an include file and want to set the
    > onLoad attribute during Page_Load time.
    >
    > Thanks,
    >
    > Tom.
    >
    >
     
    Karl Seguin, Dec 13, 2004
    #3
  4. tshad

    tshad Guest

    "bruce barker" <> wrote in message
    news:...
    > just use javascript:
    >
    > <script>document.body.onload = function()
    > {document.forms[0].txtLogon.focus();};</script>


    I tried that in a small html file to test it and it doesn't seem to work (at
    least not the way I did it).

    ****************************************************************
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <script language="javascript">
    document.body.onload = function(){document.forms[0].txtEmail.focus();};
    </script>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    Email message: <input name="txtEmail" type="text" size="32" id="txtEmail" />

    <body>
    </body>
    </html>
    *****************************************************************

    Am I missing something?

    Thanks,

    Tom.

    >
    > -- bruce (sqlwork.com)
    >
    >
    > "tshad" <> wrote in message
    > news:...
    > | Is there a way during Page_Load to change or add an attribute to the
    > Body
    > | tag?
    > |
    > | I want to be able to change the onLoad body attribute to do a focus on
    > one
    > | of my text boxes, such as:
    > |
    > | onLoad="document.forms[0].txtLogon.focus();"
    > |
    > | The problem is I have my <body> in an include file and want to set the
    > | onLoad attribute during Page_Load time.
    > |
    > | Thanks,
    > |
    > | Tom.
    > |
    > |
    >
    >
     
    tshad, Dec 13, 2004
    #4
  5. tshad

    tshad Guest

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > There are ways, but why not just use Page.RegisterStartupScript?
    >
    > Dim str As New System.Text.StringBuilder
    > str.Append("<script language=""JavaScript"">")
    > str.Append(System.Environment.NewLine)
    > str.Append("document.forms[0].txtLogon.focus();")
    > str.Append(System.Environment.NewLine)
    > str.Append("</script>")
    > Page.RegisterStartupScript("SetFocus", str.ToString())


    That is a great idea. I didn't know this existed. I started looking into
    how this works and tried to create a small page that really does nothing,
    but I wanted to look at how the RegisterStartupScript works. I am running
    into the same error I have had before whenever I try to put a tag into a
    string. I usually get an error.

    Here is the page I am using:

    *********************************************************************************
    <%@ Page Language="VB" trace="false" debug="true" AutoEventWireup="true"
    ContentType="text/html" ResponseEncoding="iso-8859-1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@ Import Namespace="System.Web.Mail" %>
    <html>
    <script runat="server">
    sub sendEmail_click ( sender as Object, e as EventArgs )
    Call setFocus(txtLogon)
    End Sub

    Private Sub SetFocus(ByVal ctrl As Control)
    ' Define the JavaScript function for the specified control.
    Dim focusScript As String = "<script language='javascript'>" & _
    "document.getElementById('" + ctrl.ClientID & "').focus();</script>"

    ' Add the JavaScript code to the page.
    Page.RegisterStartupScript("FocusScript", focusScript)
    End Sub

    </script>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Home Page</title>
    <link href="staffing.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <form id="Form1" runat="server">
    <center>
    <br>
    <table width="500" border="0" cellspacing="0" cellpadding="2">
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td width="114" colspan=2><span class="style1">Simply enter your email
    address below and we'll email you your password. </span></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td align="right">Email Address: </td>
    <td><asp:textbox id="txtLogon" runat="server" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value=" Send "
    onClick="sendEmail_click">&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    <br>
    </center>
    </form>
    </body>
    </html>
    *******************************************************************************

    I get the following error:
    *******************************************************************
    Compiler Error Message: BC30648: String constants must end with a double
    quote.

    Source Error:

    Line 11: ' Define the JavaScript function for the specified control.
    Line 12: Dim focusScript As String = "<script language='javascript'>" &
    _
    Line 13: "document.getElementById('" + ctrl.ClientID &
    "').focus();</script>"
    Line 14:
    Line 15: ' Add the JavaScript code to the page.
    **************************************************************************

    If I take out any character from "</script>" (doesn't matter which
    character), I don't get the error.

    So it obviously has nothing to do with the double quotes.

    Why does this happen?

    I am not even into the RegisterStartupScript yet, until I can solve this
    question.

    Thanks,

    Tom
    >
    > Nice function to have in a utility class...
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "tshad" <> wrote in message
    > news:...
    >> Is there a way during Page_Load to change or add an attribute to the Body
    >> tag?
    >>
    >> I want to be able to change the onLoad body attribute to do a focus on
    >> one
    >> of my text boxes, such as:
    >>
    >> onLoad="document.forms[0].txtLogon.focus();"
    >>
    >> The problem is I have my <body> in an include file and want to set the
    >> onLoad attribute during Page_Load time.
    >>
    >> Thanks,
    >>
    >> Tom.
    >>
    >>

    >
    >
     
    tshad, Dec 13, 2004
    #5
  6. tshad

    Karl Seguin Guest

    Just a bug :)
    http://support.microsoft.com/kb/316174/EN-US/

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "tshad" <> wrote in message
    news:...
    > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    > wrote in message news:...
    > > There are ways, but why not just use Page.RegisterStartupScript?
    > >
    > > Dim str As New System.Text.StringBuilder
    > > str.Append("<script language=""JavaScript"">")
    > > str.Append(System.Environment.NewLine)
    > > str.Append("document.forms[0].txtLogon.focus();")
    > > str.Append(System.Environment.NewLine)
    > > str.Append("</script>")
    > > Page.RegisterStartupScript("SetFocus", str.ToString())

    >
    > That is a great idea. I didn't know this existed. I started looking into
    > how this works and tried to create a small page that really does nothing,
    > but I wanted to look at how the RegisterStartupScript works. I am running
    > into the same error I have had before whenever I try to put a tag into a
    > string. I usually get an error.
    >
    > Here is the page I am using:
    >
    >

    ****************************************************************************
    *****
    > <%@ Page Language="VB" trace="false" debug="true" AutoEventWireup="true"
    > ContentType="text/html" ResponseEncoding="iso-8859-1" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > <%@ Import Namespace="System.Web.Mail" %>
    > <html>
    > <script runat="server">
    > sub sendEmail_click ( sender as Object, e as EventArgs )
    > Call setFocus(txtLogon)
    > End Sub
    >
    > Private Sub SetFocus(ByVal ctrl As Control)
    > ' Define the JavaScript function for the specified control.
    > Dim focusScript As String = "<script language='javascript'>" & _
    > "document.getElementById('" + ctrl.ClientID & "').focus();</script>"
    >
    > ' Add the JavaScript code to the page.
    > Page.RegisterStartupScript("FocusScript", focusScript)
    > End Sub
    >
    > </script>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    > <title>Home Page</title>
    > <link href="staffing.css" rel="stylesheet" type="text/css">
    > </head>
    > <body>
    > <form id="Form1" runat="server">
    > <center>
    > <br>
    > <table width="500" border="0" cellspacing="0" cellpadding="2">
    > <tr>
    > <td>&nbsp;</td>
    > <td>&nbsp;</td>
    > </tr>
    > <tr>
    > <td width="114" colspan=2><span class="style1">Simply enter your email
    > address below and we'll email you your password. </span></td>
    > </tr>
    > <tr>
    > <td>&nbsp;</td>
    > <td>&nbsp;</td>
    > </tr>
    > <tr>
    > <td align="right">Email Address: </td>
    > <td><asp:textbox id="txtLogon" runat="server" /></td>
    > </tr>
    > <tr>
    > <td>&nbsp;</td>
    > <td>&nbsp;</td>
    > </tr>
    > <tr>
    > <td>&nbsp;</td>
    > <td><input type="submit" name="Submit" value=" Send "
    > onClick="sendEmail_click">&nbsp;</td>
    > </tr>
    > <tr>
    > <td>&nbsp;</td>
    > <td>&nbsp;</td>
    > </tr>
    > </table>
    > <br>
    > </center>
    > </form>
    > </body>
    > </html>
    >

    ****************************************************************************
    ***
    >
    > I get the following error:
    > *******************************************************************
    > Compiler Error Message: BC30648: String constants must end with a double
    > quote.
    >
    > Source Error:
    >
    > Line 11: ' Define the JavaScript function for the specified control.
    > Line 12: Dim focusScript As String = "<script language='javascript'>"

    &
    > _
    > Line 13: "document.getElementById('" + ctrl.ClientID &
    > "').focus();</script>"
    > Line 14:
    > Line 15: ' Add the JavaScript code to the page.
    > **************************************************************************
    >
    > If I take out any character from "</script>" (doesn't matter which
    > character), I don't get the error.
    >
    > So it obviously has nothing to do with the double quotes.
    >
    > Why does this happen?
    >
    > I am not even into the RegisterStartupScript yet, until I can solve this
    > question.
    >
    > Thanks,
    >
    > Tom
    > >
    > > Nice function to have in a utility class...
    > >
    > > Karl
    > >
    > > --
    > > MY ASP.Net tutorials
    > > http://www.openmymind.net/
    > >
    > >
    > > "tshad" <> wrote in message
    > > news:...
    > >> Is there a way during Page_Load to change or add an attribute to the

    Body
    > >> tag?
    > >>
    > >> I want to be able to change the onLoad body attribute to do a focus on
    > >> one
    > >> of my text boxes, such as:
    > >>
    > >> onLoad="document.forms[0].txtLogon.focus();"
    > >>
    > >> The problem is I have my <body> in an include file and want to set the
    > >> onLoad attribute during Page_Load time.
    > >>
    > >> Thanks,
    > >>
    > >> Tom.
    > >>
    > >>

    > >
    > >

    >
    >
     
    Karl Seguin, Dec 13, 2004
    #6
  7. tshad

    tshad Guest

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:OmN$...
    > Just a bug :)
    > http://support.microsoft.com/kb/316174/EN-US/


    I just love MS.

    They say it is by design. One of those undocumented feature, I suppose.

    What is interesting is that I see examples on the net all the time that
    build strings that is supposed to dynamically put the Javascript on a page
    and they "never" do this (add the "chr(60) &" in place of the "<"). I
    wonder why that is.

    Thanks,

    Tom.
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "tshad" <> wrote in message
    > news:...
    >> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    >> wrote in message news:...
    >> > There are ways, but why not just use Page.RegisterStartupScript?
    >> >
    >> > Dim str As New System.Text.StringBuilder
    >> > str.Append("<script language=""JavaScript"">")
    >> > str.Append(System.Environment.NewLine)
    >> > str.Append("document.forms[0].txtLogon.focus();")
    >> > str.Append(System.Environment.NewLine)
    >> > str.Append("</script>")
    >> > Page.RegisterStartupScript("SetFocus", str.ToString())

    >>
    >> That is a great idea. I didn't know this existed. I started looking
    >> into
    >> how this works and tried to create a small page that really does nothing,
    >> but I wanted to look at how the RegisterStartupScript works. I am
    >> running
    >> into the same error I have had before whenever I try to put a tag into a
    >> string. I usually get an error.
    >>
    >> Here is the page I am using:
    >>
    >>

    > ****************************************************************************
    > *****
    >> <%@ Page Language="VB" trace="false" debug="true" AutoEventWireup="true"
    >> ContentType="text/html" ResponseEncoding="iso-8859-1" %>
    >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    >> <%@ Import Namespace="System.Web.Mail" %>
    >> <html>
    >> <script runat="server">
    >> sub sendEmail_click ( sender as Object, e as EventArgs )
    >> Call setFocus(txtLogon)
    >> End Sub
    >>
    >> Private Sub SetFocus(ByVal ctrl As Control)
    >> ' Define the JavaScript function for the specified control.
    >> Dim focusScript As String = "<script language='javascript'>" & _
    >> "document.getElementById('" + ctrl.ClientID &
    >> "').focus();</script>"
    >>
    >> ' Add the JavaScript code to the page.
    >> Page.RegisterStartupScript("FocusScript", focusScript)
    >> End Sub
    >>
    >> </script>
    >> <head>
    >> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    >> <title>Home Page</title>
    >> <link href="staffing.css" rel="stylesheet" type="text/css">
    >> </head>
    >> <body>
    >> <form id="Form1" runat="server">
    >> <center>
    >> <br>
    >> <table width="500" border="0" cellspacing="0" cellpadding="2">
    >> <tr>
    >> <td>&nbsp;</td>
    >> <td>&nbsp;</td>
    >> </tr>
    >> <tr>
    >> <td width="114" colspan=2><span class="style1">Simply enter your
    >> email
    >> address below and we'll email you your password. </span></td>
    >> </tr>
    >> <tr>
    >> <td>&nbsp;</td>
    >> <td>&nbsp;</td>
    >> </tr>
    >> <tr>
    >> <td align="right">Email Address: </td>
    >> <td><asp:textbox id="txtLogon" runat="server" /></td>
    >> </tr>
    >> <tr>
    >> <td>&nbsp;</td>
    >> <td>&nbsp;</td>
    >> </tr>
    >> <tr>
    >> <td>&nbsp;</td>
    >> <td><input type="submit" name="Submit" value=" Send "
    >> onClick="sendEmail_click">&nbsp;</td>
    >> </tr>
    >> <tr>
    >> <td>&nbsp;</td>
    >> <td>&nbsp;</td>
    >> </tr>
    >> </table>
    >> <br>
    >> </center>
    >> </form>
    >> </body>
    >> </html>
    >>

    > ****************************************************************************
    > ***
    >>
    >> I get the following error:
    >> *******************************************************************
    >> Compiler Error Message: BC30648: String constants must end with a double
    >> quote.
    >>
    >> Source Error:
    >>
    >> Line 11: ' Define the JavaScript function for the specified control.
    >> Line 12: Dim focusScript As String = "<script language='javascript'>"

    > &
    >> _
    >> Line 13: "document.getElementById('" + ctrl.ClientID &
    >> "').focus();</script>"
    >> Line 14:
    >> Line 15: ' Add the JavaScript code to the page.
    >> **************************************************************************
    >>
    >> If I take out any character from "</script>" (doesn't matter which
    >> character), I don't get the error.
    >>
    >> So it obviously has nothing to do with the double quotes.
    >>
    >> Why does this happen?
    >>
    >> I am not even into the RegisterStartupScript yet, until I can solve this
    >> question.
    >>
    >> Thanks,
    >>
    >> Tom
    >> >
    >> > Nice function to have in a utility class...
    >> >
    >> > Karl
    >> >
    >> > --
    >> > MY ASP.Net tutorials
    >> > http://www.openmymind.net/
    >> >
    >> >
    >> > "tshad" <> wrote in message
    >> > news:...
    >> >> Is there a way during Page_Load to change or add an attribute to the

    > Body
    >> >> tag?
    >> >>
    >> >> I want to be able to change the onLoad body attribute to do a focus on
    >> >> one
    >> >> of my text boxes, such as:
    >> >>
    >> >> onLoad="document.forms[0].txtLogon.focus();"
    >> >>
    >> >> The problem is I have my <body> in an include file and want to set the
    >> >> onLoad attribute during Page_Load time.
    >> >>
    >> >> Thanks,
    >> >>
    >> >> Tom.
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >
     
    tshad, Dec 13, 2004
    #7
    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. Roger

    Add attribute to Body

    Roger, Feb 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,130
    Roger
    Feb 6, 2004
  2. Mark Rae

    Add an attribute to the tag <body>?

    Mark Rae, Oct 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Gustavo Arriola
    Oct 31, 2006
  3. London Boy
    Replies:
    2
    Views:
    356
    Keith Bowes
    Jan 12, 2004
  4. jdi
    Replies:
    2
    Views:
    138
  5. Replies:
    5
    Views:
    236
    A. Sinan Unur
    Sep 8, 2005
Loading...

Share This Page