Generated JavaScript vs. Manually Created JavaScript: Which one comes first?

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Nov 7, 2007.

  1. There are many cases in which I want to use the same event for manually
    added JavaScript as one that is used for generated JavaScript. For example,
    when I set the AutoPostBack property of a TextBox to True, the JavaScript
    onchange event is used. However, I may want to execute a piece of JavaScript
    before the postback, such as setting the value to 0 if the user changes the
    value to "" (Otherwise, I would recieve an error when trying to use a
    function such as CInt()). Situations like this also occur quite often when
    using Validators with Display="Dynamic". Is there any way to control what
    order the JavaScript is added to the event? Does anybody have any
    suggestions on a workaround (that doesn't require checking the value in
    every place in my code that I use the value)? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Nov 7, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    bruce barker Guest

    Re: Generated JavaScript vs. Manually Created JavaScript: Which onecomes first?

    the system generated javascript calls are added when the control is
    rendered. they are appended to any code specified in the attribute.

    the real issue is most of the controls are not client script friendly.
    i.e the textbox does not support a onclientchange, or onclientblur, you
    have to set the attributes and hope the control passes it on. you also
    need to reverse engineer (usually via view source) which events are used.

    -- bruce (sqlwork.com)

    Nathan Sokalski wrote:
    > There are many cases in which I want to use the same event for manually
    > added JavaScript as one that is used for generated JavaScript. For example,
    > when I set the AutoPostBack property of a TextBox to True, the JavaScript
    > onchange event is used. However, I may want to execute a piece of JavaScript
    > before the postback, such as setting the value to 0 if the user changes the
    > value to "" (Otherwise, I would recieve an error when trying to use a
    > function such as CInt()). Situations like this also occur quite often when
    > using Validators with Display="Dynamic". Is there any way to control what
    > order the JavaScript is added to the event? Does anybody have any
    > suggestions on a workaround (that doesn't require checking the value in
    > every place in my code that I use the value)? Thanks.
    bruce barker, Nov 7, 2007
    #2
    1. Advertising

  3. Hi,

    yes, you can add any client script event by adding it as an attribute
    of any control and it will be rendered properly by ASP.NET.

    When ASP.NET is rendering the control it writes any code defined
    attribute first, appending any automatically generated code
    afterwards.

    The code defined attribute is copyed verbatim to the browser. You can
    even prevent the automatic code to run adding a cold "return;" at the
    end of your event scriptlet.

    However, the best way to validate the user entry is at the
    Page_Validate event handler, where you can verify every entry of the
    user (if you are using AJAX.NET you need to rethink this approach).

    At this event you can set the text (or value property) of every user
    input and then do not worry about it anywhere in your page.

    Just make sure to set the CausesValidation property of the button that
    will do some work.

    Regards,

    Paulo Santos
    http://pjondevelopment.50webs.com

    On Nov 6, 9:57 pm, "Nathan Sokalski" <> wrote:
    > There are many cases in which I want to use the same event for manually
    > added JavaScript as one that is used for generated JavaScript. For example,
    > when I set the AutoPostBack property of a TextBox to True, the JavaScript
    > onchange event is used. However, I may want to execute a piece of JavaScript
    > before the postback, such as setting the value to 0 if the user changes the
    > value to "" (Otherwise, I would recieve an error when trying to use a
    > function such as CInt()). Situations like this also occur quite often when
    > using Validators with Display="Dynamic". Is there any way to control what
    > order the JavaScript is added to the event? Does anybody have any
    > suggestions on a workaround (that doesn't require checking the value in
    > every place in my code that I use the value)? Thanks.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    PJ on Development, Nov 7, 2007
    #3
  4. Nathan Sokalski

    Nick Chan Guest

    hi i don't know if this works in PageLoad, but this works in
    LoadComplete


    Private Sub haha(ByVal o As Object, ByVal e As EventArgs) Handles
    Me.LoadComplete
    If Not IsPostBack Then
    ttt.Attributes("onchange") = "if(this.value=='')this.value=0;" &
    ttt.Attributes("onchange")
    End If
    End Sub

    On Nov 7, 8:57 am, "Nathan Sokalski" <> wrote:
    > There are many cases in which I want to use the same event for manually


    > added JavaScript as one that is used for generated JavaScript. For example,
    > when I set the AutoPostBack property of a TextBox to True, the JavaScript
    > onchange event is used. However, I may want to execute a piece of JavaScript
    > before the postback, such as setting the value to 0 if the user changes the
    > value to "" (Otherwise, I would recieve an error when trying to use a
    > function such as CInt()). Situations like this also occur quite often when
    > using Validators with Display="Dynamic". Is there any way to control what
    > order the JavaScript is added to the event? Does anybody have any
    > suggestions on a workaround (that doesn't require checking the value in
    > every place in my code that I use the value)? Thanks.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/
    Nick Chan, Nov 7, 2007
    #4
  5. Nick,

    Yes, it would work on Page_Load, or anywhere else in the code, prior
    the call to the Render event.

    The rendering of a page occurs at the very last moment in the life of
    a page, just prior to GC.

    Regards,

    Paulo Santos
    http://pjondevelopment.50webs.com

    On Nov 7, 6:58 am, Nick Chan <> wrote:
    > hi i don't know if this works in PageLoad, but this works in
    > LoadComplete
    >
    > Private Sub haha(ByVal o As Object, ByVal e As EventArgs) Handles
    > Me.LoadComplete
    > If Not IsPostBack Then
    > ttt.Attributes("onchange") = "if(this.value=='')this.value=0;" &
    > ttt.Attributes("onchange")
    > End If
    > End Sub
    >
    > On Nov 7, 8:57 am, "Nathan Sokalski" <> wrote:
    >
    >
    >
    > > There are many cases in which I want to use the same event for manually
    > > added JavaScript as one that is used for generated JavaScript. For example,
    > > when I set the AutoPostBack property of a TextBox to True, the JavaScript
    > > onchange event is used. However, I may want to execute a piece of JavaScript
    > > before the postback, such as setting the value to 0 if the user changes the
    > > value to "" (Otherwise, I would recieve an error when trying to use a
    > > function such as CInt()). Situations like this also occur quite often when
    > > using Validators with Display="Dynamic". Is there any way to control what
    > > order the JavaScript is added to the event? Does anybody have any
    > > suggestions on a workaround (that doesn't require checking the value in
    > > every place in my code that I use the value)? Thanks.
    > > --
    > > Nathan Sokalski
    > > ://www.nathansokalski.com/- Hide quoted text -

    >
    > - Show quoted text -
    PJ on Development, Nov 8, 2007
    #5
    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. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    !!web control not recognised1 what comes first?

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 29, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    459
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=
    Nov 29, 2004
  2. Soren Kuula
    Replies:
    2
    Views:
    543
    Malcolm Dew-Jones
    Apr 13, 2005
  3. Jason Sachs

    which comes first?

    Jason Sachs, Feb 15, 2007, in forum: C++
    Replies:
    2
    Views:
    309
    Dave Rahardja
    Feb 16, 2007
  4. Jack Dowson

    Chick and egg,which comes first?

    Jack Dowson, Sep 16, 2007, in forum: Java
    Replies:
    18
    Views:
    561
  5. Nathan Sokalski
    Replies:
    4
    Views:
    554
    PJ on Development
    Nov 8, 2007
Loading...

Share This Page