Wednesday, March 28, 2012

Why this code does not work when its button is inside the AJAX “UpdatePanel” control ?

protectedvoid DownlFile_Click(object sender,EventArgs e)

{

string web_path ="./ items/part1.pdf";

string path = Server.MapPath(web_path);

FileStream MyFileStream =

newFileStream(path,FileMode.Open);

long FileSize;

FileSize = MyFileStream.Length;

byte[] Buffer =newbyte[(int)FileSize];

MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);

MyFileStream.Close();

Response.ContentType ="application/pdf";

Response.AddHeader

("content-disposition","attachment; filename=part1.pdf");

Response.BinaryWrite(Buffer);

}

Hi paradise_wolf,

You can't use Response.Write in a AsyncPostback trigger event of an UpdatePanel. You need move the button outside the UpdatePanel or register it as Postback trigger.

Hope this helps,


Hispvlong

I am not sure if "RegisterAsyncPostBackControl" registers the button asPostback triggeras you suggested to do. I am getting this error message:

"Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near '%PDF-1.3%□□3389 0'."

Below is the relevant ASP.NET web form code and the added C# code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

protectedvoid Page_Load(object sender,EventArgs e)

{

ScriptManager1.RegisterAsyncPostBackControl(DownlFile);

..

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<asp:UpdatePanelID="ITEMS_LIST_EREADER_PANEL" runat="server"

UpdateMode="Conditional">

<ContentTemplate>

..

<asp:ButtonID="EreaderBtn"runat="server"Text="Next"

OnClick="EreaderBtn_Click"/>

..

</ContentTemplate>

</asp:UpdatePanel>

<asp:UpdatePanelID="DOWNLOAD_PANEL"UpdateMode="Conditional"

runat="server">

<ContentTemplate>

..

<tableID="DownloadDialogTb"visible="false"runat="server">

<tr>

<tdalign="center">

<asp:ButtonID="DownlFile"runat="server"Text="Download"

OnClick="DownlFile_Click"/>

</td>

</tr>

</table>

..

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTriggerControlId="EreaderBtn"

EventName="Click"/>

</Triggers>

</asp:UpdatePanel>


Hi paradise_wolf,

You should use RegisterPostBackControl method to register a control as Postback trigger.

Hope this helps,


Thank you very much,spvlong . Big Smile

hi,

I am also getting same problem while generating PDF? i could not solver even after registring as postback trigger control. It gives me the same error.

If you have solved it, plz suggest the solution for the same.

thanks

Varsha


http://forums.asp.net/1662626/ShowThread.aspx#1662626

No comments:

Post a Comment