What's the REAL deal with Javascript ?

Discussion in 'ASP .Net' started by Liz, Oct 7, 2006.

  1. Liz

    Liz Guest

    Do you really have to use these methods like RegisterClientScriptBlock() to
    get client-side Javascript into an ASP.NET 2.0 page ? For anything beyond
    trivial scripting, this is really a painful process, no ?

    I guess I'm not clear why you can't put JS inline ? Doesn't it just get
    emitted along with any literal HTML in your page ??

    Is there a way to load an external xxx.js file into your code ?

    Thanks for any input ...

    Liz
     
    Liz, Oct 7, 2006
    #1
    1. Advertising

  2. Liz

    Jon Paal Guest

    you can use all the normal js stuff...


    "Liz" <> wrote in message news:...
    > Do you really have to use these methods like RegisterClientScriptBlock() to
    > get client-side Javascript into an ASP.NET 2.0 page ? For anything beyond
    > trivial scripting, this is really a painful process, no ?
    >
    > I guess I'm not clear why you can't put JS inline ? Doesn't it just get
    > emitted along with any literal HTML in your page ??
    >
    > Is there a way to load an external xxx.js file into your code ?
    >
    > Thanks for any input ...
    >
    > Liz
    >
    >
     
    Jon Paal, Oct 7, 2006
    #2
    1. Advertising

  3. Liz

    Liz Guest

    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:OJbG$...
    > you can use all the normal js stuff...


    really ? so what's with the Page methods like "RegisterClientScriptBlock()"
    ?

    L



    > "Liz" <> wrote in message

    news:...
    > > Do you really have to use these methods like RegisterClientScriptBlock()

    to
    > > get client-side Javascript into an ASP.NET 2.0 page ? For anything

    beyond
    > > trivial scripting, this is really a painful process, no ?
    > >
    > > I guess I'm not clear why you can't put JS inline ? Doesn't it just get
    > > emitted along with any literal HTML in your page ??
    > >
    > > Is there a way to load an external xxx.js file into your code ?
    > >
    > > Thanks for any input ...
    > >
    > > Liz
    > >
    > >

    >
    >
     
    Liz, Oct 7, 2006
    #3
  4. Liz

    Mark Rae Guest

    "Liz" <> wrote in message
    news:%...

    > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    > news:OJbG$...
    >> you can use all the normal js stuff...

    >
    > really ?


    Yes.

    > so what's with the Page methods like "RegisterClientScriptBlock()"


    It allows you to create dynamic JavaScript.
     
    Mark Rae, Oct 7, 2006
    #4
  5. "Liz" <> wrote in message
    news:...
    > Do you really have to use these methods like RegisterClientScriptBlock()
    > to
    > get client-side Javascript into an ASP.NET 2.0 page ? For anything beyond
    > trivial scripting, this is really a painful process, no ?


    No, you do not HAVE to do anything. You can write code that does a lot of
    things without having any emission of code. The issues comes when you start
    working with server controls, as you either have to render, find the name
    and write the script (or) emit to get the rendered name of the control.

    > I guess I'm not clear why you can't put JS inline ? Doesn't it just get
    > emitted along with any literal HTML in your page ??


    Yes. It is largely for naming of controls without having to worry about
    where they are on the page.

    > Is there a way to load an external xxx.js file into your code ?


    Yes, you can link it in the HTML.

    If you write your routines so they take the control as a parameter and then
    emit the control specific calls, you end up with the best of both worlds.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com/

    *************************************************
    Think Outside the Box!
    *************************************************
     
    Cowboy \(Gregory A. Beamer\), Oct 7, 2006
    #5
  6. Liz

    Liz Guest

    "Cowboy (Gregory A. Beamer)" <> wrote in
    message news:...
    >
    > "Liz" <> wrote in message
    > news:...
    > > Do you really have to use these methods like RegisterClientScriptBlock()
    > > to
    > > get client-side Javascript into an ASP.NET 2.0 page ? For anything

    beyond
    > > trivial scripting, this is really a painful process, no ?

    >
    > No, you do not HAVE to do anything. You can write code that does a lot of
    > things without having any emission of code. The issues comes when you

    start
    > working with server controls,


    ok ... so if I have a Grid and I want to swap CSS styles on rows when
    onmouseover fires, what then ? I can't just delegate to a method in the
    attribute because it'll run on the server ... is there a pattern to do this
    on the client ?

    L
     
    Liz, Oct 7, 2006
    #6
  7. Liz

    Jon Paal Guest

    for a grid, you could add an event handler....

    '*******************************************************
    Sub ReportGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    '
    ' The ReportGrid_ItemDataBound helper method is used to
    ' highlight rows in grid table from mouse actions
    '
    '*******************************************************

    If e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem Then

    If e.Item.ItemType = ListItemType.Item Then
    '---------------------------------------------------
    ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
    '---------------------------------------------------
    e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'")
    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='White'")
    End If

    If e.Item.ItemType = ListItemType.AlternatingItem Then
    '---------------------------------------------------
    ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
    '---------------------------------------------------
    e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'")
    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='CornSilk'")
    End If

    'We can add the onclick event handler
    e.Item.Attributes.Add("onclick","this.style.backgroundColor='lightblue'")
    End If

    End Sub








    "Liz" <> wrote in message news:...
    >
    > "Cowboy (Gregory A. Beamer)" <> wrote in
    > message news:...
    >>
    >> "Liz" <> wrote in message
    >> news:...
    >> > Do you really have to use these methods like RegisterClientScriptBlock()
    >> > to
    >> > get client-side Javascript into an ASP.NET 2.0 page ? For anything

    > beyond
    >> > trivial scripting, this is really a painful process, no ?

    >>
    >> No, you do not HAVE to do anything. You can write code that does a lot of
    >> things without having any emission of code. The issues comes when you

    > start
    >> working with server controls,

    >
    > ok ... so if I have a Grid and I want to swap CSS styles on rows when
    > onmouseover fires, what then ? I can't just delegate to a method in the
    > attribute because it'll run on the server ... is there a pattern to do this
    > on the client ?
    >
    > L
    >
    >
     
    Jon Paal, Oct 7, 2006
    #7
  8. Liz

    Liz Guest

    cool ... thanks :)

    Liz


    :: but wouldn't it have been easier/nicer if MS had opened up the server tag
    attributes spec a bit so you could do something like:

    <asp:Control runat="server" client::eek:nmouseover="abc"
    client::eek:nmouseout="xyz" ... /> ... or some such thing ... ?



    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:...
    > for a grid, you could add an event handler....
    >
    > '*******************************************************
    > Sub ReportGrid_ItemDataBound(ByVal sender As Object, ByVal e As

    DataGridItemEventArgs)
    > '
    > ' The ReportGrid_ItemDataBound helper method is used to
    > ' highlight rows in grid table from mouse actions
    > '
    > '*******************************************************
    >
    > If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =

    ListItemType.AlternatingItem Then
    >
    > If e.Item.ItemType = ListItemType.Item Then
    > '---------------------------------------------------
    > ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
    > '---------------------------------------------------
    > e.Item.Attributes.Add("onmouseover",

    "this.style.backgroundColor='Silver'")
    > e.Item.Attributes.Add("onmouseout",

    "this.style.backgroundColor='White'")
    > End If
    >
    > If e.Item.ItemType = ListItemType.AlternatingItem Then
    > '---------------------------------------------------
    > ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
    > '---------------------------------------------------
    > e.Item.Attributes.Add("onmouseover",

    "this.style.backgroundColor='Silver'")
    > e.Item.Attributes.Add("onmouseout",

    "this.style.backgroundColor='CornSilk'")
    > End If
    >
    > 'We can add the onclick event handler
    >

    e.Item.Attributes.Add("onclick","this.style.backgroundColor='lightblue'")
    > End If
    >
    > End Sub
    >
    >
    >
    >
    >
    >
    >
    >
    > "Liz" <> wrote in message

    news:...
    > >
    > > "Cowboy (Gregory A. Beamer)" <> wrote

    in
    > > message news:...
    > >>
    > >> "Liz" <> wrote in message
    > >> news:...
    > >> > Do you really have to use these methods like

    RegisterClientScriptBlock()
    > >> > to
    > >> > get client-side Javascript into an ASP.NET 2.0 page ? For anything

    > > beyond
    > >> > trivial scripting, this is really a painful process, no ?
    > >>
    > >> No, you do not HAVE to do anything. You can write code that does a lot

    of
    > >> things without having any emission of code. The issues comes when you

    > > start
    > >> working with server controls,

    > >
    > > ok ... so if I have a Grid and I want to swap CSS styles on rows when
    > > onmouseover fires, what then ? I can't just delegate to a method in the
    > > attribute because it'll run on the server ... is there a pattern to do

    this
    > > on the client ?
    > >
    > > L
    > >
    > >

    >
    >
     
    Liz, Oct 7, 2006
    #8
  9. Hi,

    Liz wrote:
    > cool ... thanks :)
    >
    > Liz
    >
    >
    > :: but wouldn't it have been easier/nicer if MS had opened up the server tag
    > attributes spec a bit so you could do something like:
    >
    > <asp:Control runat="server" client::eek:nmouseover="abc"
    > client::eek:nmouseout="xyz" ... /> ... or some such thing ... ?


    They did.

    <asp:ListBox Runat="server"
    ID="lsbWhichTreeview"
    AutoPostBack="True"
    onchange="alert('Test');">

    ....

    </asp:ListBox>

    generates this client side code:

    <select size="4" name="lsbWhichTreeview"
    onchange="alert('Test');setTimeout('__doPostBack(\'lsbWhichTreeview\',\'\')',
    0)" language="javascript" id="lsbWhichTreeview">

    ....

    </select>

    Note the inclusion of the client-side code before the "__doPostBack"
    code. I rather recommend against this way, because it worsens the code
    readibility. I prefer to do that in the code behind, using the
    "Attributes" collection.

    I usually add JavaScript to my HTML ages or custom controls using
    JavaScript files (always recommended, better encapsulation, easier to
    debug, etc...). In the code behind, I use the Attributes collection to
    set the event handlers. And I use the ClientScript manager only for
    dynamic JavaScript code, which is usually limited to setting "constants"
    (JavaScript doesn't have them, I use normal variables for that), and for
    localization (setting strings that I get from the resource files).

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, GalaSoft, Oct 8, 2006
    #9
  10. Liz

    Mark Rae Guest

    Mark Rae, Oct 8, 2006
    #10
  11. Hi,

    Mark Rae wrote:
    > "Laurent Bugnion, GalaSoft" <> wrote in message
    > news:...
    >
    >
    >>which is usually limited to setting "constants" (JavaScript doesn't have
    >>them, I use normal variables for that)

    >
    >
    > http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:const


    The same page also states

    "const is a Mozilla-specific extension, it is not supported by IE or Opera."

    But I agree that I should have been more precise.

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, GalaSoft, Oct 8, 2006
    #11
  12. Liz

    Liz Guest

    "Laurent Bugnion, GalaSoft" <> wrote in message
    news:...

    >> :: but wouldn't it have been easier/nicer if MS had opened up the server
    >> tag
    >> attributes spec a bit so you could do something like:
    >>
    >> <asp:Control runat="server" client::eek:nmouseover="abc"
    >> client::eek:nmouseout="xyz" ... /> ... or some such thing ... ?

    >
    > They did.
    >
    > <asp:ListBox Runat="server"
    > ID="lsbWhichTreeview"
    > AutoPostBack="True"
    > onchange="alert('Test');">



    it's limited ... basically to events where server-side processing might make
    sense .... you don't see a mouseover event for ListBox, right ?
     
    Liz, Oct 8, 2006
    #12
  13. Hi,

    Liz wrote:
    > "Laurent Bugnion, GalaSoft" <> wrote in message
    > news:...
    >
    >
    >>>:: but wouldn't it have been easier/nicer if MS had opened up the server
    >>>tag
    >>>attributes spec a bit so you could do something like:
    >>>
    >>><asp:Control runat="server" client::eek:nmouseover="abc"
    >>>client::eek:nmouseout="xyz" ... /> ... or some such thing ... ?

    >>
    >>They did.
    >>
    >><asp:ListBox Runat="server"
    >>ID="lsbWhichTreeview"
    >>AutoPostBack="True"
    >>onchange="alert('Test');">

    >
    >
    >
    > it's limited ... basically to events where server-side processing might make
    > sense .... you don't see a mouseover event for ListBox, right ?


    One more reason to use the Attributes collection instead. Honestly, in
    all my years of ASP.NET programming, I never once added a client-side
    event handler to an ASP.NET control tag.

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, GalaSoft, Oct 8, 2006
    #13
  14. Liz

    Mark Rae Guest

    "Laurent Bugnion, GalaSoft" <> wrote in message
    news:...

    > One more reason to use the Attributes collection instead. Honestly, in all
    > my years of ASP.NET programming, I never once added a client-side event
    > handler to an ASP.NET control tag.


    Me neither.
     
    Mark Rae, Oct 9, 2006
    #14
    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. senthil
    Replies:
    5
    Views:
    1,389
    senthil
    Jan 24, 2004
  2. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    551
    Duncan Smith
    Dec 28, 2005
  3. Curious Trigger
    Replies:
    2
    Views:
    1,815
    Curious Trigger
    Sep 9, 2006
  4. FAQ server
    Replies:
    1
    Views:
    84
    Dr J R Stockton
    Jan 23, 2007
  5. FAQ server
    Replies:
    10
    Views:
    155
    Dr J R Stockton
    Mar 29, 2007
Loading...

Share This Page