Search code examples
asp.nethttpresponseresponse.write

Response.End doesnot render javascript


On the click of the button my Onclick event gets fired. Inside the onclick event I generate somefile at runtime and render it to the browser in the following way. But before rendering it to the browser I am making particular Label visible. But still the Label never become visible. Any idea what is the problem

    lblInfoMessage.Visible=true;
    Response.ContentType = "text/plain";
    Response.AppendHeader("Content-Disposition", "attachment; filename=test.gxml");
    doc.Save(Response.OutputStream);
    Response.End();

Solution

  • You can either refresh the page (and thus change visibility or contents of controls) or send an attachment. Not both. So you will have to find some other way, maybe client side javascript?

    EDIT
    In the button you need a OnClientClick in the server side code, this will translate into a client-side "onclick". Here you can call a javascript function where you can (for instance) display some text. Note that this function is executed before the submit action that will generate the file.

    Something like this in the html/aspx:

    <span id="infoMessage"><!-- empty --></span>
    ...
    <asp:Button OnClientClick="showInfo()" ... />
    ...
    <script type="text/javascript">
      function showInfo() {
        document.getElementById("infoMessage").innerText = 
            "This is the info message.";
    
      }
    </script>
    

    You can't just show that Label that you have now, as that doesn't exist in the client-side html when it is invisible.