• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1145
  • 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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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