Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encoding a url string 1

Status
Not open for further replies.

gregmosu

Programmer
Jul 8, 2002
117
US
I am using javascript to open a new window, and the url that I'm sending always comes thru to the new page w/the ampersands stripped out.

Here is the code that opens the new page.
Code:
Dim s1 As String = "batch/batchPdf.aspx?type=candGar&GroupId=" & GroupId & "&ClientId=" & ClientId & "&CandId=" & CandId
			Response.Write("<script>window.open('" & s1 & "','_new','scrollbars,location,toolbar,width=800,height=300');</script>")

I tried using Server.URLEncode(), and a number of escape sequences that I thought might work.. but all failed. Any help would be greatly appreciated.

Thanks,
Greg
 
Dim s1 As String = "batch/batchPdf.aspx?type=candGar&GroupId=" & Server.URLEncode(GroupId)& "&ClientId=" & Server.URLEncode(ClientId) & "&CandId=" & Server.URLEncode(CandId)

Response.Write("<script>window.open('" & JSEncode(s1) & "','_new','scrollbars,location,toolbar,width=800,height=300');</script>")



Public Function JSEncode(ByVal s As String) As String
s = Replace(s, "\", "\\")
s = Replace(s, "'", "\'")
s = Replace(s, """", "\""")
s = Replace(s, Chr(10), "\n")
s = Replace(s, Chr(13), "\r")
Return s
End Function
 
Sorry, now I see what you are talking about.
Create a Literal either on the ASPX or in Code Behind.
Should stay away from Response.Write when in code behind.


Dim s1 As String = "batch/batchPdf.aspx?type=candGar&GroupId=" & Server.URLEncode(GroupId)& "&ClientId=" & Server.URLEncode(ClientId) & "&CandId=" & Server.URLEncode(CandId)

dim lit as new Literal
lit.text="<script>window.open('" & JSEncode(s1) & "','_new','scrollbars,location,toolbar,width=800,height=300');</script>"
Me.Controls.Add(lit)



Public Function JSEncode(ByVal s As String) As String
s = Replace(s, "\", "\\")
s = Replace(s, "'", "\'")
s = Replace(s, """", "\""")
s = Replace(s, Chr(10), "\n")
s = Replace(s, Chr(13), "\r")
Return s
End Function
 
Tried it, but I'm getting this error message...

System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

I take it to mean that I need to move all the code into the code behind page then?

-Greg
 
There is a trick to writing script in the ASPX page when you are not in code behind.

s="<" & "script>some javascript here</" & script>"
Sounds like you are trying to use ASP.Net to write ASP 3.0 style of writing. It is best to break this bad habit and take advantage of .net's OOP.

The example works when done correctly in code behind.
 
To be honest, I used to be a java developer. This is one of the first few asp.net applications I've written, and I dont fully know what I'm doing yet. So the code behind page kinda like a servlet that specifically attached to that aspx page?

Anyways, thanks for all your help!
 
Actually, it is just conveniently attached to the ASPX page.
The code behind is just another class like any other.
It could actually be in an entirely different DDL setting in the bin folder. You could have many aspx pages using your one vb or cs class if you wanted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top