• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1136
  • Last Modified:

difference between Attributes.Add() and Response.write to execute " Javascript"

I am having following problem:

Response.Write("<script>" + "window.location.href=window.showModalDialog('/popopOffer.aspx');" + "</script>");
when I use this code on the button1_Click() event it displays the popop and when I close the popop it doesn't redirect to the required page - goes to the parent page.

 btnCheckout.Attributes.Add("OnClick", "window.location.href=window.showModalDialog('/popopOffer.aspx');");  
When I use this code the button1_Click() event it displays the popop but keeps the background blank(can't see the content of the parent page) but redirects to the required page.

the code on the popup page as follows:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script language="javascript" type="text/javascript">
// <!CDATA[
function button1_onclick()
window.returnValue = "http://CheckoutPage.aspx";

// ]]>
    <form id="form1" runat="server">
        <center><asp:Image ID="Image2" runat="server" /></center><br /><br />
          <div><asp:Localize ID="Offer" runat="server" meta:resourcekey="Offer" /></div><br /><br />
        <center><asp:Image ID="Image1" runat="server" /></center><br /><br />
           <div><asp:Localize ID="OfferDescription" runat="server" meta:resourcekey="OfferDescription" /></div><br />
          <center><input id="button1" type="button" value="Yes,Tell me more"  onclick="button1_onclick()"/>


Any help will be appreciated.
1 Solution
The different is:

when you use response.write this piece of code will be added after the page is render,
So when you finished compiling on the server side (the response.write code is executed), after rendered, this script:
<script>" + "window.location.href=window.showModalDialog('/popopOffer.aspx');" + "</script> will be executed.

when you add attribute to the button server btnCheckOut
btnCheckout.Attributes.Add("OnClick", "window.location.href=window.showModalDialog('/popopOffer.aspx');");  

This will add the CLIENT CLICK for this button (same as you do OnClientClick), and this mean, this javascript will be executed when btnCheckOut is clicked. ALL HAPPEN in Client side. (so the page doesn't postback execute the javascript first)

Notice that this is different behaviour from normal server button click (it posted back to server, server run Inint, Page_Load, then btnCheckOut_Click(object sender...)

Hope this helps


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now