Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.Write and UpdatePanel

I generate a vcard that I send to the client using the following code snippet:

Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileNameOnly));
Response.ContentType = "text/x-vcard";
Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
Response.Write(vCard.ToString());
Response.End();

However, I need to use vCards on a page that has the control inside and UpdatePanel. Unfortunately, according to Update panel and Response write this does not work and causes an error. I am wondering what are some alternative ways to send the contents of the vcard/file to the client's browser and have it display "open/save" dialog that don't involve Response.Write?

like image 362
laconicdev Avatar asked Apr 04 '12 16:04

laconicdev


People also ask

What is an UpdatePanel?

UpdatePanel controls are a central part of AJAX functionality in ASP.NET. They are used with the ScriptManager control to enable partial-page rendering. Partial-page rendering reduces the need for synchronous postbacks and complete page updates when only part of the page has to be updated.

What is trigger in UpdatePanel?

Triggers for a given UpdatePanel, by default, automatically include any child controls that invoke a postback, including (for example) TextBox controls that have their AutoPostBack property set to true.

Can we use multiple UpdatePanel in asp net?

By using multiple UpdatePanel controls on a page, you can incrementally update regions of the page separately or together. For more information about partial-page updates, see Partial-Page Rendering Overview and Introduction to the UpdatePanel Control.


4 Answers

You can't use Response.Write during an asynchronous postback. Whatever control executes that code needs to be added as a PostBackTrigger in the update panel:

<Triggers>        
    <asp:PostBackTrigger ControlID="Button1" />
</Triggers>

You can also do it in code-behind, if you prefer:

ScriptManager.GetCurrent().RegisterPostBackControl(Button1);
like image 77
James Johnson Avatar answered Oct 03 '22 20:10

James Johnson


Response.Write will not work under Asynchronous Events. My suggestion is to remove the Update Panel in case it is specifically being used for VCard point of view only.

Alternatively - Place a control inside the Update Panel and initialize it's value under asynchronous event. Now it will work.

like image 43
Pankaj Avatar answered Oct 03 '22 22:10

Pankaj


Why don't you consider the use of a separate handler/page to serve the vcard?

This is maybe the easiest and cleaner way to do that and it doesnt interfere any other (async or not) postback related to the updatepanel.

like image 39
Alex Pollan Avatar answered Oct 03 '22 20:10

Alex Pollan


I had a similar problem with Response.Write. I found a workaround or maybe even a solution to this problem. Capture the TextWriter given to the RenderBeginTag of a server control and write to that.

I blogged with an example here: http://timscyclingblog.wordpress.com/2013/03/07/asp-net-web-forms-response-write-in-an-updatepanel-dev-web/

like image 40
user2000095-tim Avatar answered Oct 03 '22 21:10

user2000095-tim