gpt4 book ai didi

c# - 避免在使用 onload=document.form.submit() 将 html 写入 HttpContext.Current.Response 后返回两次

转载 作者:行者123 更新时间:2023-11-30 18:39:23 24 4
gpt4 key购买 nike

这是一个很长的标题,这就是我正在做的事情:为了避免在 URL 中显示报告参数,我在代码隐藏的按钮单击处理程序中执行此操作以显示报告:

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>");

这很好用,但是当我从报告中返回时,我被带到了我刚刚生成的页面,由于 onload 事件,该页面立即提交了表单。我可以非常快地反击两次以回到过去,但这并不理想。

在编写 html 之前,我尝试使用 ClientScript.RegisterStartupScript 使用 JavaScript 打开一个新窗口,但我不知道如何(或者是否可能)获取新窗口的 HttpContext。

最佳答案

我无法使用您的代码重现问题,因此更多上下文可能会有所帮助。根据页面其余部分的结构,您可以使用 PostbackURL 而不是点击处理程序:

<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>

这样您就不必担心浏览器历史记录中的额外/替换页面。

关于c# - 避免在使用 onload=document.form.submit() 将 html 写入 HttpContext.Current.Response 后返回两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10020509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com