Does the ASP.Net Panel client side onLoad work?

Discussion in 'ASP .Net' started by =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 19, 2006.

  1. I'm trying to create a web page and I need a javascript function to be called
    on the load of a particular panel. (The panel is hidden during some but not
    all postbacks.)

    The function is bound to the panel in VB as follows:

    pnlAdd.Attributes.Add("onLoad", "SetupScreenLite()")

    This is basically how we attach all functions in our projects but this is
    the first time I've worked with onLoad and the first time I've tried to
    attach anything to a panel. Any ideas?
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 19, 2006
    #1
    1. Advertising

  2. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news:D:

    > I'm trying to create a web page and I need a javascript function
    > to be called on the load of a particular panel. (The panel is
    > hidden during some but not all postbacks.)
    >
    > The function is bound to the panel in VB as follows:
    >
    > pnlAdd.Attributes.Add("onLoad", "SetupScreenLite()")
    >
    > This is basically how we attach all functions in our projects
    > but this is the first time I've worked with onLoad and the first
    > time I've tried to attach anything to a panel. Any ideas?



    The panel control generates a <DIV> tag. That tag does not support
    the onload event:

    http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/div.asp

    In fact, the onload event is valid for only a small selection of
    HTML tags:

    http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onload.asp

    Using the page's RegisterStartupScript method may be an
    alternative way to achieve your goal.


    --
    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 19, 2006
    #2
    1. Advertising

  3. Ok, the problem is this. I am a relative beginner in Javascript. I am
    trying to develop an ASP.net 1.1 web page. I need a way for a Javascript
    function to fire once after the load of the page into the browser. What are
    my options?

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news:D:
    >
    > > I'm trying to create a web page and I need a javascript function
    > > to be called on the load of a particular panel. (The panel is
    > > hidden during some but not all postbacks.)
    > >
    > > The function is bound to the panel in VB as follows:
    > >
    > > pnlAdd.Attributes.Add("onLoad", "SetupScreenLite()")
    > >
    > > This is basically how we attach all functions in our projects
    > > but this is the first time I've worked with onLoad and the first
    > > time I've tried to attach anything to a panel. Any ideas?

    >
    >
    > The panel control generates a <DIV> tag. That tag does not support
    > the onload event:
    >
    > http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/div.asp
    >
    > In fact, the onload event is valid for only a small selection of
    > HTML tags:
    >
    > http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onload.asp
    >
    > Using the page's RegisterStartupScript method may be an
    > alternative way to achieve your goal.
    >
    >
    > --
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 19, 2006
    #3
  4. add the onload attribute to the page's BODY tag:

    <BODY onload="myJsFunction();" >

    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "B. Chernick" wrote:

    > Ok, the problem is this. I am a relative beginner in Javascript. I am
    > trying to develop an ASP.net 1.1 web page. I need a way for a Javascript
    > function to fire once after the load of the page into the browser. What are
    > my options?
    >
    > "Chris R. Timmons" wrote:
    >
    > > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > > <> wrote in
    > > news:D:
    > >
    > > > I'm trying to create a web page and I need a javascript function
    > > > to be called on the load of a particular panel. (The panel is
    > > > hidden during some but not all postbacks.)
    > > >
    > > > The function is bound to the panel in VB as follows:
    > > >
    > > > pnlAdd.Attributes.Add("onLoad", "SetupScreenLite()")
    > > >
    > > > This is basically how we attach all functions in our projects
    > > > but this is the first time I've worked with onLoad and the first
    > > > time I've tried to attach anything to a panel. Any ideas?

    > >
    > >
    > > The panel control generates a <DIV> tag. That tag does not support
    > > the onload event:
    > >
    > > http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/div.asp
    > >
    > > In fact, the onload event is valid for only a small selection of
    > > HTML tags:
    > >
    > > http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onload.asp
    > >
    > > Using the page's RegisterStartupScript method may be an
    > > alternative way to achieve your goal.
    > >
    > >
    > > --
    > > Hope this helps.
    > >
    > > Chris.
    > > -------------
    > > C.R. Timmons Consulting, Inc.
    > > http://www.crtimmonsinc.com/
    > >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 19, 2006
    #4
  5. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news::

    > Ok, the problem is this. I am a relative beginner in
    > Javascript. I am trying to develop an ASP.net 1.1 web page. I
    > need a way for a Javascript function to fire once after the load
    > of the page into the browser. What are my options?


    As Peter noted, the onload attribute of the <BODY> tag can be
    used, as can the RegisterStartupScript method.

    Personally, I prefer to use RegisterStartupScript. It's
    possible that some browsers have buggy implementations of
    onload, plus I can insert as much JavaScript as I want
    with RegisterStartupScript. JavaScript inserted into the
    onload attribute is realistically limited to method calls and
    short segments of code that can be expressed in one line.

    More info on client-side scripting w/ ASP.Net:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/clientsidescript.asp


    --
    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 19, 2006
    #5
  6. You refer to 'buggy implementations of onload'.

    Could this, in practice, result in one or more web controls appearing as
    nulls (and crashing the script) when the onload function fires, even if the
    controls have been 'wired' into the Javascript in a conventional fashion?
    (i.e. document.getElementById("<%= {control-name}.ClientID %>");

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news::
    >
    > > Ok, the problem is this. I am a relative beginner in
    > > Javascript. I am trying to develop an ASP.net 1.1 web page. I
    > > need a way for a Javascript function to fire once after the load
    > > of the page into the browser. What are my options?

    >
    > As Peter noted, the onload attribute of the <BODY> tag can be
    > used, as can the RegisterStartupScript method.
    >
    > Personally, I prefer to use RegisterStartupScript. It's
    > possible that some browsers have buggy implementations of
    > onload, plus I can insert as much JavaScript as I want
    > with RegisterStartupScript. JavaScript inserted into the
    > onload attribute is realistically limited to method calls and
    > short segments of code that can be expressed in one line.
    >
    > More info on client-side scripting w/ ASP.Net:
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/clientsidescript.asp
    >
    >
    > --
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 20, 2006
    #6
  7. A worse problem, or perhaps I have misstated the problem. I was testing with
    the body onload event and it appears that this event only fires once on
    initial load and does not fire on postbacks. I was assuming otherwise. I
    need an event that fires on every postback.

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news::
    >
    > > Ok, the problem is this. I am a relative beginner in
    > > Javascript. I am trying to develop an ASP.net 1.1 web page. I
    > > need a way for a Javascript function to fire once after the load
    > > of the page into the browser. What are my options?

    >
    > As Peter noted, the onload attribute of the <BODY> tag can be
    > used, as can the RegisterStartupScript method.
    >
    > Personally, I prefer to use RegisterStartupScript. It's
    > possible that some browsers have buggy implementations of
    > onload, plus I can insert as much JavaScript as I want
    > with RegisterStartupScript. JavaScript inserted into the
    > onload attribute is realistically limited to method calls and
    > short segments of code that can be expressed in one line.
    >
    > More info on client-side scripting w/ ASP.Net:
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/clientsidescript.asp
    >
    >
    > --
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 20, 2006
    #7
  8. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news::

    > You refer to 'buggy implementations of onload'.
    >
    > Could this, in practice, result in one or more web controls
    > appearing as nulls (and crashing the script) when the onload
    > function fires, even if the controls have been 'wired' into the
    > Javascript in a conventional fashion? (i.e.
    > document.getElementById("<%= {control-name}.ClientID %>");


    I don't think so. Is that line of JavaScript before or after the
    control in question? The control needs to appear before the
    JavaScript that accesses it so the browser can put the control in the
    page's document object model (DOM) tree.

    For example, this HTML will generate an "object required" error
    message, because the <INPUT> control comes after the JavaScript
    that's trying to access that control's value:

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <SCRIPT>
    var value = document.getElementById("myInputBox").value;
    alert(value);
    </SCRIPT>
    <INPUT type="text" id="myInputBox" name="myInputBox"
    value="Hello, world!"/>
    </BODY>
    </HTML>

    But placing the <INPUT> control before the JavaScript will work:

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <INPUT type="text" id="myInputBox" name="myInputBox"
    value="Hello, world!"/>
    <SCRIPT>
    var value = document.getElementById("myInputBox").value;
    alert(value);
    </SCRIPT>
    </BODY>
    </HTML>

    This is where the RegisterStartupScript method comes in handy. It
    always places the JavaScript at the very end of the HTML, just before
    the closing </FORM> tag.

    If that's not the problem, what might be happening is either "name
    mangling" of the ClientID by ASP.Net, or a known bug in .Net 1.1:

    http://support.microsoft.com/default.aspx?id=818803

    Concerning name mangling, when multiple instances of the same user
    control are present on a page, they cannot all have the same ID in
    the generated HTML page. Therefore, ASP.Net gives each instance of
    the control a unique name when it generates the HTML.

    A control named BlogSideBar1, with an embedded textbox named
    SearchBox, would be referenced by C#/VB in an .ascx file as
    BlogSideBar1.SearchBox. However, in the HTML it would appear as
    _ctl0____ctl0___BlogSideBar1___SearchBox.

    Your line of code should work, depending on when it's executed in the
    page's life cycle on the server. If there are multiple instances of
    "control-name" in your page, that situation might be causing some
    kind of problem.

    I generally don't put inline <% %> tags in my pages, except for
    mundane things like the page's title or meta tags. I prefer to build
    the JavaScript, complete with ClientIDs, in my code behind files.
    That way I feel I have better control over exactly what gets sent to
    the browser, and where it's placed in the page.

    --
    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 21, 2006
    #8
  9. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news::

    > A worse problem, or perhaps I have misstated the problem. I was
    > testing with the body onload event and it appears that this
    > event only fires once on initial load and does not fire on
    > postbacks. I was assuming otherwise. I need an event that fires
    > on every postback.


    Are you assigning the value to the onload event in the page's
    Page_Load event? If so, is that assignment in an if/then statement
    that uses IsPostBack?

    It sounds like what's happening is this:

    ::In the Page_Load event::

    if not IsPostBack then
    assign value to <BODY> onload
    end if

    The fix is to move the onload assignment outside of that if/then
    block, so it's always executed when the page is initially created,
    and after every postback.

    --
    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 21, 2006
    #9
  10. Thanks, but help me out a little more (Javascript beginner, remember?) I'm
    drawing a blank.

    When you say 'assign value to <BODY> onload' , are you referring to
    something like RegisterStartUpScript or is this yet another Javascript
    technique I haven't seen yet?

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news::
    >
    > > A worse problem, or perhaps I have misstated the problem. I was
    > > testing with the body onload event and it appears that this
    > > event only fires once on initial load and does not fire on
    > > postbacks. I was assuming otherwise. I need an event that fires
    > > on every postback.

    >
    > Are you assigning the value to the onload event in the page's
    > Page_Load event? If so, is that assignment in an if/then statement
    > that uses IsPostBack?
    >
    > It sounds like what's happening is this:
    >
    > ::In the Page_Load event::
    >
    > if not IsPostBack then
    > assign value to <BODY> onload
    > end if
    >
    > The fix is to move the onload assignment outside of that if/then
    > block, so it's always executed when the page is initially created,
    > and after every postback.
    >
    > --
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 21, 2006
    #10
  11. On second thought, are you refering to giving the body an ID, declaring it in
    the codebehind, and then doing an <idname>.Attributes.Add("onLoad" etc?

    Just found an example and I can see it in the HTML source but it doesn't fire.

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news::
    >
    > > A worse problem, or perhaps I have misstated the problem. I was
    > > testing with the body onload event and it appears that this
    > > event only fires once on initial load and does not fire on
    > > postbacks. I was assuming otherwise. I need an event that fires
    > > on every postback.

    >
    > Are you assigning the value to the onload event in the page's
    > Page_Load event? If so, is that assignment in an if/then statement
    > that uses IsPostBack?
    >
    > It sounds like what's happening is this:
    >
    > ::In the Page_Load event::
    >
    > if not IsPostBack then
    > assign value to <BODY> onload
    > end if
    >
    > The fix is to move the onload assignment outside of that if/then
    > block, so it's always executed when the page is initially created,
    > and after every postback.
    >
    > --
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 21, 2006
    #11
  12. Sorry about the number of appends. I moved my code up in the Page_Load
    codebehind so that it always gets called but the HTML code only fires the
    first time, never on postback. (but I can still see it in the HTML source)

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news::
    >
    > > A worse problem, or perhaps I have misstated the problem. I was
    > > testing with the body onload event and it appears that this
    > > event only fires once on initial load and does not fire on
    > > postbacks. I was assuming otherwise. I need an event that fires
    > > on every postback.

    >
    > Are you assigning the value to the onload event in the page's
    > Page_Load event? If so, is that assignment in an if/then statement
    > that uses IsPostBack?
    >
    > It sounds like what's happening is this:
    >
    > ::In the Page_Load event::
    >
    > if not IsPostBack then
    > assign value to <BODY> onload
    > end if
    >
    > The fix is to move the onload assignment outside of that if/then
    > block, so it's always executed when the page is initially created,
    > and after every postback.
    >
    > --
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 21, 2006
    #12
  13. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news::

    > Sorry about the number of appends. I moved my code up in the
    > Page_Load codebehind so that it always gets called but the HTML
    > code only fires the first time, never on postback. (but I can
    > still see it in the HTML source)


    That sounds strange. Could you post some code that demonstrates the
    problem?

    Thanks.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 21, 2006
    #13
  14. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news::

    > On second thought, are you refering to giving the body an ID,
    > declaring it in the codebehind, and then doing an
    > <idname>.Attributes.Add("onLoad" etc?


    Yes.

    The body tag should look something like this in the .aspx file:

    <BODY runat="server" id="body">

    Declare it as an HtmlControl in the code behind file:

    protected System.Web.UI.HtmlControls.HtmlGenericControl body;

    And the onload attribute can be assigned like this in the Page_Load
    event handler:

    this.body.Attributes["onload"] = "alert('Hello, world!');";

    > Just found an example and I can see it in the HTML source but it
    > doesn't fire.


    Does the body tag have the runat="server" attribute?

    --
    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 21, 2006
    #14
  15. Not much to show.

    In my aspx file I have <body id="body1" runat="server" >

    In my vb file I have the declaration: ' Protected WithEvents body1 As
    HtmlGenericControl' (I have tried this both with and without the WithEvents
    declaration. It apparently has no effect on results.)

    In the page_load (above the usual test for postback, so it's always called)
    I have: 'body1.Attributes.Add("onLoad", "alert('test');")'

    In the HTML source of the postback screen I see: '<body id="body1"
    onLoad="alert('test');">'

    But it never fires except on the very first time.

    "Chris R. Timmons" wrote:

    > "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    > <> wrote in
    > news::
    >
    > > Sorry about the number of appends. I moved my code up in the
    > > Page_Load codebehind so that it always gets called but the HTML
    > > code only fires the first time, never on postback. (but I can
    > > still see it in the HTML source)

    >
    > That sounds strange. Could you post some code that demonstrates the
    > problem?
    >
    > Thanks.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 21, 2006
    #15
  16. "B. Chernick" wrote:

    > Not much to show.
    >
    > In my aspx file I have <body id="body1" runat="server" >
    >
    > In my vb file I have the declaration: ' Protected WithEvents body1 As
    > HtmlGenericControl' (I have tried this both with and without the WithEvents
    > declaration. It apparently has no effect on results.)
    >
    > In the page_load (above the usual test for postback, so it's always called)
    > I have: 'body1.Attributes.Add("onLoad", "alert('test');")'
    >
    > In the HTML source of the postback screen I see: '<body id="body1"
    > onLoad="alert('test');">'
    >
    > But it never fires except on the very first time.


    I'm pretty much out of ideas. If possible, could you post a complete .aspx
    page that demonstrates the problem?

    Thanks.

    --
    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    =?Utf-8?B?Q2hyaXMgUi4gVGltbW9ucw==?=, Feb 21, 2006
    #16
  17. I should apologize for wasting your time. I've been looking in the wrong
    place. If you set up a test screen with only the code I have described it
    works perfectly every time. I suspect that the third party (Telerik)
    controls we're using are somehow interfering with the postback.

    "Chris R. Timmons" wrote:

    > "B. Chernick" wrote:
    >
    > > Not much to show.
    > >
    > > In my aspx file I have <body id="body1" runat="server" >
    > >
    > > In my vb file I have the declaration: ' Protected WithEvents body1 As
    > > HtmlGenericControl' (I have tried this both with and without the WithEvents
    > > declaration. It apparently has no effect on results.)
    > >
    > > In the page_load (above the usual test for postback, so it's always called)
    > > I have: 'body1.Attributes.Add("onLoad", "alert('test');")'
    > >
    > > In the HTML source of the postback screen I see: '<body id="body1"
    > > onLoad="alert('test');">'
    > >
    > > But it never fires except on the very first time.

    >
    > I'm pretty much out of ideas. If possible, could you post a complete .aspx
    > page that demonstrates the problem?
    >
    > Thanks.
    >
    > --
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
    >
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Feb 21, 2006
    #17
  18. "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
    <> wrote in
    news::

    > I should apologize for wasting your time. I've been looking in
    > the wrong place. If you set up a test screen with only the code
    > I have described it works perfectly every time. I suspect that
    > the third party (Telerik) controls we're using are somehow
    > interfering with the postback.


    No problem.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Feb 21, 2006
    #18
    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. Fabio R.
    Replies:
    0
    Views:
    776
    Fabio R.
    Mar 25, 2005
  2. Boss302
    Replies:
    0
    Views:
    1,018
    Boss302
    Nov 21, 2006
  3. Geoff Davis
    Replies:
    3
    Views:
    175
    Geoff Davis
    Jan 15, 2004
  4. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    529
    Martin Honnen
    Nov 4, 2004
  5. Replies:
    5
    Views:
    250
    Thomas 'PointedEars' Lahn
    May 15, 2005
Loading...

Share This Page