What's the REAL deal with Javascript ?

  • Thread starter Thread starter Liz
  • Start date Start date
L

Liz

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 said:
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) said:
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
 
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
 
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::onmouseover="abc"
client::onmouseout="xyz" ... /> ... or some such thing ... ?



Jon Paal said:
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
 
Hi,
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::onmouseover="abc"
client::onmouseout="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
 
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 ?
 
Hi,
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
 
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.
 
Back
Top