Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么打印弹出窗口会在打印助手中自动打开和关闭?_C#_Asp.net - Fatal编程技术网

C# 为什么打印弹出窗口会在打印助手中自动打开和关闭?

C# 为什么打印弹出窗口会在打印助手中自动打开和关闭?,c#,asp.net,C#,Asp.net,我正在使用Response.Write编写脚本,它只在Response.End()上执行;但是在那之后,我的执行停止了,并且它没有在asp.net c中提到的页面上重定向。我对asp.net c非常陌生,希望我能把查询弄清楚。请帮忙 下面我分享我的PRINTHELPER类 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using S

我正在使用Response.Write编写脚本,它只在Response.End()上执行;但是在那之后,我的执行停止了,并且它没有在asp.net c中提到的页面上重定向。我对asp.net c非常陌生,希望我能把查询弄清楚。请帮忙

下面我分享我的PRINTHELPER类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Web.SessionState;

public class PrintHelper
{
   public PrintHelper()
  {

  }

public static void PrintWebControl(Control ctrl)
{
    PrintWebControl(ctrl, string.Empty);
}

public static void PrintWebControl(Control ctrl, string Script)
{
    StringWriter stringWrite = new StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    if (ctrl is WebControl)
    {
        Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
    }
    Page pg = new Page();        

    pg.EnableEventValidation = false;
    if (Script != string.Empty)
    {
        pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
    }

    HtmlLink link = new HtmlLink();
    link.Attributes.Add("href", "bootstrap/css/bootstrap.css");
    link.Attributes.Add("type", "text/css");
    link.Attributes.Add("rel", "stylesheet");
    pg.Controls.Add(link);
    HtmlForm frm = new HtmlForm();
    pg.Controls.Add(frm);

    frm.Attributes.Add("runat", "server");
    frm.Controls.Add(ctrl);
    pg.DesignerInitialize();
    pg.RenderControl(htmlWrite);
    string strHTML = stringWrite.ToString();
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Write(strHTML);
    HttpContext.Current.Response.Write("<script>window.print();</script>");
    HttpContext.Current.Response.Write("<script>window.location.href = 
   'default.aspx'</script>");
    HttpContext.Current.Response.End();
 }
}
使用系统;
使用系统数据;
使用系统配置;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
使用System.IO;
使用系统文本;
使用System.Web.SessionState;
公共类PrintHelper
{
公共PrintHelper()
{
}
公共静态无效PrintWebControl(控件ctrl)
{
PrintWebControl(ctrl,string.Empty);
}
公共静态无效PrintWebControl(控件ctrl、字符串脚本)
{
StringWriter stringWrite=新建StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite=新的System.Web.UI.HtmlTextWriter(stringWrite);
如果(ctrl为网络控制)
{
单位w=新单位(100,UnitType.Percentage);((网络控制)ctrl).Width=w;
}
第pg页=新页();
pg.EnableEventValidation=false;
if(脚本!=string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(),“PrintJavaScript”,Script);
}
HTMLINK link=新HTMLINK();
link.Attributes.Add(“href”,“bootstrap/css/bootstrap.css”);
link.Attributes.Add(“type”、“text/css”);
添加(“rel”,“样式表”);
pg.Controls.Add(链接);
HtmlForm frm=新的HtmlForm();
pg.Controls.Add(frm);
添加(“runat”、“server”);
frm.Controls.Add(ctrl);
第页设计初始化();
pg.RenderControl(htmlWrite);
string strHTML=stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write(“window.print();”);
HttpContext.Current.Response.Write(“window.location.href=
“default.aspx'”;
HttpContext.Current.Response.End();
}
}

我所做的是在打印后将其重定向到同一页面,即在打印后使用

只需替换此代码:

HttpContext.Current.Response.Write("<script>
window.location.href = 'default.aspx'</script>");
HttpContext.Current.Response.Write(“
window.location.href='default.aspx');
使用此代码:

HttpContext.Current.Response.Write("<script>
window.onafterprint = function(){window.location.href = 'default.aspx'}</script>");
HttpContext.Current.Response.Write(“
window.onafterprint=function(){window.location.href='default.aspx'});

注释掉最后两行的关闭和重定向(看看是否一切正常)@Aristos,Tnx进行回复,在注释后它可以工作,但当我按F5时,表单重新发送警报框出现,并在按“继续”时再次插入相同的值两次。@Aristos,因此,我决定在打印完成后将其重定向到同一页上,但它对我不起作用。