I’m having trouble getting my ASP.Net page to return CSV on submit being clicked. Here’s the asp button definition I have for the submit button in Form.aspx:
<asp:Button id="submitreport" name="submitbutton" text="submit" OnClick="Report_Submit" runat="server" />
And this is the corresponding function in Form.aspx.cs:
public void Report_Submit(object sender, EventArgs e) {
Debug.WriteLine("GETS HERE?");
Response.Charset = "UTF-8";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetAllowResponseInBrowserHistory(true);
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment; filename='" + DateTime.Today.ToString() + ".csv'");
Response.Write("test,output");
Response.End();
}
When this is ran in Visual Studio I don’t even see the debug print line. Does anybody know what’s wrong with my setup?
Edit: I know for certain that the pages are set up right because if I put a breakpoint on my empty Page_Load function in Form.aspx.cs VS does break there. Besides that the breakpoint at the debug write line is skipped over on the form submit and the same page is returned again.
Update By creating a new project with just the button and the handler the Report_Submit() function is called and the CSV file is correctly generated. Since that narrows it down a little bit, does anybody know what could be going on in my other VS 2008 project that’s causing this not to work?
I found a pretty good work around that returns a .csv file (which also shouldn’t be quoted in my header creating function calls, but that wasn’t the problem). In addition, the shouldn’t specify a name since ASP.NET fills that in, but that also wasn’t the problem.
Moving just the relevant form processing code into its own project worked just fine, but my project was too large to manage that.
The workaround I found useful is to just move the button handling function into Page_Load() and use:
to distinguish page requests (GET) from users entering the form (POST requests).