Monday, March 26, 2012

AJAX and window.open dos not work

Hi,

I Have Page that uses AJAX Update panel and with in the panel i have button that contans the following

Response.Write("<script>window.open('default2.aspx',target='new');</script>")

when i click the button exception will happen saying that can not parse from the server.

any one out there that can help me solve this problem..

Thanks

If AJAX is used in your webpage, that script doesn't work anymore.

I guess you could do like below:

<asp:Button ID="btn1" runat="server" Text="OpenNew" OnClientClick="OpenNew();"></asp:Button><input type="hidden" id="hidFlag" runat="server" value="False"/><script type="text/javascript">function OpenNew(){ setInterval('Open()', 100);}function Open(){ flag = document.getElementById('hidFlag'); if (flag) { if (flag.value !='False') { window.open('default2.aspx', target='new'); flag.value ='False'; } }}</script>Code Behind:Protected Sub btn1_Click(byval sender asObject, byval e as EventArgs)Handles btn1.Click hidFlag.value ="True"End Sub

JasonMDLi:

If AJAX is used in your webpage, that script doesn't work anymore.

I guess you could do like below:

<asp:Button ID="btn1" runat="server" Text="OpenNew" OnClientClick="OpenNew();"></asp:Button><input type="hidden" id="hidFlag" runat="server" value="False"/><script type="text/javascript">function OpenNew(){ setInterval('Open()', 100);}function Open(){ flag = document.getElementById('hidFlag'); if (flag) { if (flag.value !='False') { window.open('default2.aspx', target='new'); flag.value ='False'; } }}</script>Code Behind:Protected Sub btn1_Click(byval sender asObject, byval e as EventArgs)Handles btn1.Click hidFlag.value ="True"End Sub

I've got the same issue asazuomar. What if the url for the new window is not fixed but has parameters, let's say, window.open('default2.aspx?query=4323',target='new'), and the "query" parameter has the value from some calculation that occurs inside the event btn1_Click (in your sample). What then?


nistam:

I've got the same issue asazuomar. What if the url for the new window is not fixed but has parameters, let's say, window.open('default2.aspx?query=4323',target='new'), and the "query" parameter has the value from some calculation that occurs inside the event btn1_Click (in your sample). What then?

You can pass the value to the hidden control "hidFlag" or to another hidden control.

<asp:Button ID="btn1" runat="server" Text="OpenNew" OnClientClick="OpenNew();"></asp:Button><input type="hidden" id="hidFlag" runat="server" value="False"/><input type="hidden" id="hidFlagValue" runat="server" value=""/><script type="text/javascript">function OpenNew(){ setInterval('Open()', 100);}function Open(){ flag = document.getElementById('hidFlag'); flagvalue = document.getElementById('hidFlagValue'); if (flag && flagvalue) { if (flag.value != 'False') { window.open('default2.aspx?query='+flagvalue.value, target='new'); flag.value = 'False'; } }}</script>

Code Behind:
Protected Sub btn1_Click(byval sender asObject, byval e as EventArgs)
Handles btn1.Click
hidFlagValue.value = 4323
hidFlag.value ="True"
End Sub


Yes this works, but its not a good way of doing it, with this i will keep the client PC checking on the flag always.

if there is no other way then i will be forced to use it.

But thank you for the help, you code give me many ideas... Thanks


It works fine for me too! I just need to create as many hidden values as the number of parameters.

Thanks.


azuomar:

Yes this works, but its not a good way of doing it, with this i will keep the client PC checking on the flag always.

if there is no other way then i will be forced to use it.

But thank you for the help, you code give me many ideas... Thanks

I knew it's not the best way. However, you could declare a handle variable returned from method "setInterval" and dispose it in method "Open()".


Thanks

Hi,

hope the follwoing will help without setting up the any client side flags :)

string

openPopupJS ="openpopup();";

ClientScript.RegisterStartupScript(

this.GetType(),"openPopupJS", openPopupJS,true);

Cheers,

venky

No comments:

Post a Comment