Problems with ASP.Net 2.0 and JavaScript

R

richard

OK,

Im working with a DataList in .Net 2.0, the code shows the list when
the query has executed. I have the page feature in its own div and
display:none so its not visible until the list shows. Ive written
javascript in ASP.Net to show the DIV. The code is as follows:


jScript = "<script LANGUAGE=""javascript"">" & vbNewLine
jScript = jScript & "function ToggleDiv(id){" & vbNewLine
jScript = jScript & "var elem = document.getElementById(id);" &
vbNewLine
jScript = jScript & "if (elem)" & vbNewLine
jScript = jScript & "{" & vbNewLine
jScript = jScript & " if (elem.style.display != 'block')" &
vbNewLine
jScript = jScript & " {" & vbNewLine
jScript = jScript & " elem.style.display = 'block';"
&vbNewLine
jScript = jScript & " elem.style.visibility = 'visible';"
& vbNewLine
jScript = jScript & " }" & vbNewLine
jScript = jScript & " else" & vbNewLine
jScript = jScript & " {" & vbNewLine
jScript = jScript & " elem.style.display =
'none';" & vbNewLine
jScript = jScript & " elem.style.visibility =
'hidden';" & vbNewLine
jScript = jScript & " }" & vbNewLine
jScript = jScript & " }" & vbNewLine
jScript = jScript & "}" & vbNewLine
jScript = jScript & "ToggleDiv('Page_Div');" & vbNewLine
jScript = jScript & "</sc" & "ript>"


Response.Write(jScript)


I dont get any errors, but the javascript doesn't execute either.
Anyone have any ideas?
 
M

Mark Rae

I dont get any errors, but the javascript doesn't execute either.
Anyone have any ideas?

I'm not a VB.NET specialist, but what happens if you replace vbNewLine with
"\n"
 
S

sloan

I think you gotta register it with the page (or are you not showing that
part?)

here is a sample I had ... written in vb.net

its actually a part of a class, where the constructor has the Page ... so I
can reuse it.

that's what _currentPage is all about.


I'd get a basic thing working first.. ( just an alert or something?) before
dealing with the hidden div stuff.




Private Sub CreateTrimJS()

Dim sb As StringBuilder = New StringBuilder

sb.Append("<!-- " & vbCrLf)
sb.Append("function trim(str) {" & vbCrLf)

''http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20959311.ht
ml

''sb.Append("alert('got to trim function -->' + str);" &
vbCrLf)

sb.Append("var returnVal='';" & vbCrLf)

sb.Append("returnVal=str.replace(/^\s*|\s*$/g,"""");" &
vbCrLf)

''debug
''sb.Append("alert('str -->*' + str + '*\nreturnVal--> *' +
returnVal + '*');" & vbCrLf)


sb.Append("return returnVal;" & vbCrLf)

''close the entire function
sb.Append("}" & vbCrLf)
sb.Append(vbCrLf & "// -->" & vbCrLf)




If (Not
_currentPage.IsStartupScriptRegistered("someuniquevalueHere")) Then
'prevents a "double register"

_currentPage.RegisterStartupScript("someuniquevalueHere", sb.ToString)
End If



End Sub
 
C

cannontrodder

This works fine for me. However, It DOESN'T work if your div is AFTER
this block, it doesn't exist in the dom when you call the function with
the line:

ToggleDiv('Page_Div');

If your div is before this block, it works fine.

I advise putting the whole function in a .js file and including it into
your web page in a <script> tag between the <head> .. </head> section.
That will make it easier to read/debug. I'd then put the
"ToggleDiv('Page_Div');" call in the onload event of the <body> tag.
That way, no matter where the DIV exists, IE will only call your toggle
function once the entire page is loaded into the DOM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top