why window.open script not firing? just postback...continuation of previous post

  • Thread starter Thread starter KathyB
  • Start date Start date
K

KathyB

Hi,

In the pageload of my aspx file I have the following (in both not in
postback and is postback)...

btnList.Attributes.Add("onclick", "window.open('Scanned.aspx', 'Serial
Numbers', 'width=200, height=300');")

When sent to the browser (ie 6) it appears as:

<input type="submit" name="btnList" value="Show List" id="btnList"
onClick="window.open('Scanned.aspx', 'Serial Numbers', 'width=200,
height=300');" style=the usual asp stuff...

Please tell me why this is still posting back to the originating page
instead of opening the new window...what am I missing? I would prefer
to use the Attribute.Add collection since it is easier than having to
go change each html input element.

Thanks,

Kathy
 
Hi

Because the button is a submit btn so it submits onclick unless you cancel
it

Fix:
btnList.Attributes.Add("onclick", "window.open('Scanned.aspx', 'Serial
Numbers', 'width=200, height=300'); return false");

--
Best Regards
Vidar Petursson
==============================
Microsoft Internet Client & Controls MVP
==============================
 
Kathy,

The button is posting back because the "type" property of the <input />
tag is "submit." Try using an HtmlInputButton control, which is basically
<input type="button" id="myBtn" runat="server" value="My Button" />
and then you can access the attributes property in your Page_Load code.

If you don't want your current button, which I believe is a Button
WebControl (i.e. <asp:Button runat="server" />) then you must append "return
false;" after invoking the window.open() method:

btnList.Attributes.Add("onclick", "window.open('Scanned.aspx', 'Serial
Numbers', 'width=200, height=300'); return false;")

This will prevent the button from submitting the form.

Question: Why do you need to add this code in your code behind instead of
directly in the HTML? This is how you do that:

<input type="button" name="btnList" value="Show List" id="btnList"
onclick="window.open('Scanned.aspx', 'Serial Numbers', 'width=200,
height=300');"

Notice that I didn't use the runat="server" property.

I hope that helps.

Mario
 
Back
Top