Avoid re-returning after writing html with onload = document.form.submit () in HttpContext.Current.Response
So, for a long title, here's what I do: To avoid the report parameters appearing in the url, I do this in the button handler in the code to show the report:
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("<html><head></head>");
System.Web.HttpContext.Current.Response.Write("<body onload=\"document.mainform.submit(); \">");
System.Web.HttpContext.Current.Response.Write(string.Format(CultureInfo.InvariantCulture, "<form name=\"mainform\" method=\"post\" action=\"{0}\">", ReportURL));
foreach (string key in Params.Keys)
{
System.Web.HttpContext.Current.Response.Write(string.Format(CultureInfo.InvariantCulture, "<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", key, Params[key]));
}
System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body></html>");
This works great, but when I return from the report, I ended up on a newly created page that immediately submits the form due to the onload event. I can quickly go back to go back, but this is not ideal.
I tried to open a new window with JavaScript using ClientScript.RegisterStartupScript before writing the html, but I don't know how (or if possible) get the HttpContext for the new window.
source to share
I ended up opening a new window. I have added two small javascript functions
function openNewWin() {
$('form').attr('target', '_blank');
setTimeout('resetFormTarget()', 500);
}
function resetFormTarget() {
$('form').attr('target', '');
}
Then in the linkbutton I set
OnClientClick="openNewWin();"
OnClick still executes the code above the server side, but the response is written to a new window. The setTimeout function in the openNewWin function resets the target property of the form, so the application will function normally after returning to it.
source to share
I haven't been able to recreate the problem using your code, so more context might help. Depending on how the rest of the page is structured, you can use a PostbackURL instead of a click handler:
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="Key1" Value="Value1"/>
<asp:HiddenField runat="server" ID="Key2" Value="Value2"/>
<asp:Button runat="server" ID="button"
Text="ok" PostBackUrl="ReportURL.aspx"/>
</form>
This way you don't need to worry about the extra / replaced page in your browser history.
source to share