Share via


Response.write is not working inside of update panel

Question

Monday, December 17, 2012 6:12 AM

Hi, I want to open a file using c# by clicking a button where button in updatepanel, after opening the file i want to commit some data to the database. For this i have taken button which is in updatepanel and response.write commands. However after executingthese lines its not committing any data to the database. For this i have taken button which is in updatepanel and Response commands. However after executing these lines its not committing any data to the database. my aspx page looks like

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </ContentTemplate>
    <Triggers>
    <asp:PostBackTrigger ControlID="Button1" />
    </Triggers>
    </asp:UpdatePanel>

 and my cs file Button1_Click contains

Label1.Text += "File Opening...";


            Response.Buffer = true;
            Response.Clear();
            Response.AppendHeader("Content-Type", "text/plain");
            Response.AppendHeader("Content-disposition", "attachment; filename=147.txt");
            FileStream sourceFile = new FileStream("D:\\147_1.txt", FileMode.Open);
            long FileSize;
            FileSize = sourceFile.Length;
            byte[] getContent = new byte[(int)FileSize];
            sourceFile.Read(getContent, 0, (int)sourceFile.Length);
            sourceFile.Close();
            Response.BinaryWrite(getContent);
            Response.Flush();
            System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
            Response.Close();

Label1.Text+="File Opened...";

 

In the above code nothing of text label1.text displayed or any other data committing to database.

Please help me on committing data to database after executing Response commands which opens a file.

Thanks in advance.

All replies (8)

Thursday, December 20, 2012 5:28 AM âś…Answered

Hi All,

Thanks for all your valuable replies. I have found the solution on http://encosia.com/ajax-file-downloads-and-iframes/, where i have the implemented my solution step by step and i got my result. However, do not forget that dont add any triggers to the updatepanel if you use the above linked approach i.e. iframe


Monday, December 17, 2012 6:19 AM

response.write wont work in update panel. You need to use

ScriptManager.RegisterClientScriptBlock

Monday, December 17, 2012 6:24 AM

I would try to replace the Response.Write() code you have with ScriptManager.RegisterStartupScript() and register an "alert('Saved');" code, so something like this:

ScriptManager.RegisterStartupScript(this, GetType(), "AlertCode", "alert('Saved!');", true);

More  resolved answer

http://forums.asp.net/t/1788072.aspx/1

http://forums.asp.net/t/1752582.aspx/1


Monday, December 17, 2012 6:33 AM

Hi,

I'm requesting you to please read my requirement completely and please help me on this,

How could you replace Response.write with scriptmanager.registerclientscriptblock, Response.write is HttpRespesonse where scsript manager writes script and i want to save my data to database not alert message. and also links are not working


Monday, December 17, 2012 6:42 AM

You can also do by adding a postback trigger to your update panel

<Triggers> 

<asp:PostBackTrigger ControlID="YourControlID" /> 

</Triggers>

http://stackoverflow.com/questions/5619604/show-response-binarywrite-in-browser-save-dialog-or-open-new-window-and-close-af

http://mohyuddin.blogspot.in/2008/07/responsewritreresponsebinarywritre-and.html


Monday, December 17, 2012 6:56 AM

Hi @RameshRajendran,

Thanks for reply, please read my requirement completely. I have specified all your suggestions in my question only.


Monday, December 17, 2012 10:38 PM

I would try to replace the Response.Write() code you have with ScriptManager.RegisterStartupScript() and register an "alert('Saved');" code, sosomething like this:

ScriptManager.RegisterStartupScript(this, GetType(), "AlertCode", "alert('Saved!');", true);

You can't send a traditional octet stream file response in a partial postback because there isn't an HTTP response coming back to the browser.  It's just a string returned to the XmlHTTPRequest.

However, you can use an iframe to accomplish asynchronous file downloads.

http://tgynther.blogspot.in/2009/02/aspnet-updatepanel-and-responsewrite.html

http://forums.asp.net/t/1279838.aspx


Tuesday, December 18, 2012 12:42 AM

Hi Chetan

Thanks for your valuable reply. Could u plz provide sum examplz or code which helps me. The provided links are not helpful here. From the 2nd & 3rd links already i have implemented what they suggested. From the first link which is using an iframe for asynchronous downloads, could u plz bit more on this, the provided example from link is not much useful and its not working