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

Posted on 2008-11-03
Last Modified: 2013-12-17
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="" >
<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.
Question by:excngexpdp
    1 Comment
    LVL 10

    Accepted 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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now