I wrote two methods inside of a button click event. one for download to excel file. second one for download to text file. Both have " Response.AddHeader" codeline. i want to clear the Respond.Header() after the 1st method execution.
i also tried those things.
• response.clearcontent()
• Response.ClearHeaders()
• Response.Clear()
but not worked.

Saravanan GanesanPosted Aug 16, 2023, 9:41 AM
It seems like you're trying to clear the headers after executing the first method inside a button click event in a web application. To better help you, I'll provide a more detailed explanation and steps to achieve this.
When you use
Response.AddHeaderto set HTTP headers, these headers are sent along with the response from the server to the client. If you want to clear the headers after the first method's execution, you need to ensure that you're clearing the headers before the second method is executed.Here's a sample code structure to illustrate how you could achieve this:
protected void Button_Click(object sender, EventArgs e)
{
// Call the first method for downloading Excel file
DownloadExcelFile();
// Clear the headers after the first method
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
// Call the second method for downloading Text file
DownloadTextFile();
}
private void DownloadExcelFile()
{
// Code to generate and download Excel file
// For example:
Response.AddHeader("Content-Disposition", "attachment; filename=excel_file.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
// ... write Excel file content to Response.OutputStream ...
Response.Flush();
Response.End();
}
private void DownloadTextFile()
{
// Code to generate and download Text file
// For example:
Response.AddHeader("Content-Disposition", "attachment; filename=text_file.txt");
Response.ContentType = "text/plain";
// ... write Text file content to Response.OutputStream ...
Response.Flush();
Response.End();
}
Lahiru KalutotagePosted Aug 17, 2023, 3:03 AM
error occurred with "Response.ClearHeaders(); "
Cr BhargaviPosted Aug 16, 2023, 10:54 AM
Tahir AnsariPosted Aug 16, 2023, 9:26 AM
let me know if it is helpful & accept the answer
Tahir AnsariPosted Aug 16, 2023, 9:25 AM
Response.ClearHeaders()andResponse.Clear()to remove the headers set for the Excel download. This will essentially "reset" the response headers to their default state before you set the headers for the text file download in the second method.Abhishek YadavPosted Aug 16, 2023, 7:05 AM
To clear the Response Headers after executing the first method, you can try the following approach:
Response.ClearHeaders()method.Response.Headersproperty.Here is an example code snippet: