How to use an asp:button to create a new browser window and output contents to new window

G

Guest

Hi all, as you can see from the subject, I'm try to use an asp:button to create a new browser window and output contents to new window

But default if I do the
Response.Write("..."

The output gets sent directly to the window or frame that the button was contained in - if I was using regular anchor links I could just specify the target - i.e.
<a href="http://...." target="nameNotInCurrentFrameset">something</a

Thanks
Novice
 
E

Eliyahu Goldin

You can't do it from service side. Use client-side javascript calls
showModalDialog(...) or showModalessDialog(...)

Eliyahu

Novice said:
Hi all, as you can see from the subject, I'm try to use an asp:button to
create a new browser window and output contents to new window.
But default if I do the:
Response.Write("...")

The output gets sent directly to the window or frame that the button was
contained in - if I was using regular anchor links I could just specify the
target - i.e.:
 
A

Alan Ferrandiz Langley

Indeed you have to do use client-side code, but you actually can generate that code from server-side, al you have to do is append this code to Page.Load event handler of your ASP.NET web form.

What I do in this code is to generate the actual client side script line by line using a StringBuilder for concatenation, then I have all the script code rendered in the client HTML (immediately below the opening tag of the Page object’s <form runat=â€serverâ€> ) when the page loads using the RegisterClientScriptBlock.

Then, using the button's Attributes collection, I map the onclick client event of the ASP.NET button to the name of the script I previously generated.

This will cause you ASP.NET button to respond to the onclick client-side event by opening a new browser window, you can customize the new browser window, in my case i did it like a popup, so that I can append some parameters to the query string.

Hope this helps,
Alan Ferrandiz
MCT;MCDBA,MCSD for .NET
MSF Practitioner



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim oStringBuilder As New System.Text.StringBuilder()
oStringBuilder.Append("<SCRIPT language=""javascript"">")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" <!--")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" function popup(url) {")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" newwindow = window.open(url,'name','height=300,width=450,scrollbars=no');")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" if (window.focus) {newwindow.focus()}")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" return false;")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" }")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append(" //-->")
oStringBuilder.Append(vbNewLine)
oStringBuilder.Append("</SCRIPT>")

Page.RegisterClientScriptBlock("popup", oStringBuilder.ToString)

Button1.Attributes.Add("onClick", "return popup('popup.aspx?value=somevalue');")

End Sub
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top