ClientScript.RegisterStartScript exexuting on page reload

D

Danielle

Greetings all -

I am working on a vb .NET asp web application. I have a button that
opens a new page when clicked. It works fine; the problem is when the
user refreshes the original page, the script executes again even
though the button hasn't been clicked.

Here's the sub - the button is a standard ASP button with no
frills...

Protected Sub btnNew_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnNew.Click

Const INSERT_PAGE As String = "AddNewContact.aspx"
Dim URLArg As String = INSERT_PAGE & "?agreementID=" &
agreementID
ClientScript.RegisterStartupScript(Me.GetType, "popup",
"window.open('" & BASEURL & URLArg & "','_blank','menubar=yes')",
True)

End Sub

This is making me a little crazy - any help would be greatly
appreciated. I've seen that others have had this problem, but can't
find any solution yet...

Thanks!
Danielle
 
B

bruce barker

rendering inline openwindows is poor design. for this to work, your users
have to turn off their popup blockers. you really shoudl just attach the
javascript to the button, and not do a postback.

anyway to fix your problem, on the page render a request guid in a hidden
field and also store this on the server. when the button is clicked, check to
see if the request has already been processed, if so take some action, else
process as normal, and mark the request as processed.

-- bruce (sqlwork.com)
 
N

Nick Gilbert

Danielle,

Don't attempt to implement it this way - the vast majority of people use
pop-up blockers and it won't work. Windows opening on their own when you
haven't done anything is REALLY REALLY annoying which is why the popup
blocker was invented.

I really don't understand why you're doing it that way in the first
place. If you want a button that opens a window when you click it, just
put the window.open command in the onclick event of that button! Why are
you posting back to the *server* to open a new window client side??

You don't need any server side code for this.

<a href="javascript:window.open(<blah>)">open me</a>

Ideally, so that it works in browsers which don't have javascript enabled:

<a href="page.html" target="_blank"
onclick="javascript:window.open(<blah>); return false;">open me</a>

The return false bit prevents it from opening the window using
javascript AND navigating to the site in the background. If the user has
JS disabled, it will still open in a new window, but you won't be able
to specify the size or position of the new window.

Opening the window using client side code only will also be much quicker
as it won't postback to the server before opening the window.

Nick...
 
D

Danielle

Bruce -

Thanks for the suggestion. I'd rather do this properly but am fairly
new to programming and having to do this outside of my normal role.
Which is to say I've been using code bits I've found on the web a LOT.

When you say attach the javascript to the button I'm not sure what you
mean or how it would be implemented. Can you provide a sample? Please
note that I need to pass a parameter to the page that is loaded on the
button click.

Danielle
 
D

Danielle

Well, I'm doing it because I really don't know what I'm doing...

<asp:Button ID="btnNew" runat="server" Font-Bold="true" Font-
Names="verdana"
font-size="small" Text="Add New Contact"
onclick="window.open('www.microsoft.com'); return false;" /
<br /><br />

Give me an error "BC30456: 'window' is not a member of
'ASP.details_aspx'." (details.aspx is the name of the page I amt
trying to load).
 
N

Nick Gilbert

Danielle said:
Well, I'm doing it because I really don't know what I'm doing...

<asp:Button ID="btnNew" runat="server" Font-Bold="true" Font-
Names="verdana"
font-size="small" Text="Add New Contact"
onclick="window.open('www.microsoft.com'); return false;" /

Give me an error "BC30456: 'window' is not a member of
'ASP.details_aspx'." (details.aspx is the name of the page I amt
trying to load).


You can't (and don't need to) use an asp:Button control for this. It
will be client side function only, so you shouldn't need to use a
server-side control. Just use a normal HTML button or link.

ie
<input type=button onclick="window.open();" value="Click Me" />

The onclick event of an asp:Button control is code to be executed on the
server in VB or C# etc... The onclick event of a normal HTML button is
javascript to be run locally within the browser.

Hope this makes sense!

Nick...
 
N

Nick Gilbert

Danielle said:
Well, I'm doing it because I really don't know what I'm doing...

<asp:Button ID="btnNew" runat="server" Font-Bold="true" Font-
Names="verdana"
font-size="small" Text="Add New Contact"
onclick="window.open('www.microsoft.com'); return false;" /

Give me an error "BC30456: 'window' is not a member of
'ASP.details_aspx'." (details.aspx is the name of the page I amt
trying to load).


You can't (and don't need to) use an asp:Button control for this. It
will be client side function only, so you shouldn't need to use a
server-side control. Just use a normal HTML button or link.

ie
<input type=button onclick="window.open();" value="Click Me" />

The onclick event of an asp:Button control is code to be executed on the
server in VB or C# etc... The onclick event of a normal HTML button is
javascript to be run locally within the browser.

Hope this makes sense!

Nick...
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top