问题:u盘无法弹出 无法关闭弹出窗体以及输出到word时保留表格
描述:

我想点击主页面上的按钮打开一个弹出窗口并把这个打开窗体的页面内容输出到word中,然后自动关闭这个弹出窗口,代码如下:
主页面a的按钮事件:
private void btnOut_ServerClick(object sender, System.EventArgs e)
{
Response.Write("window.open('MappingToWord.aspx?gduid="+gduid+"')");
}
弹出窗体MappingToWord的PageLoad事件:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
//接收页面传递的的参数
string gduid = Request.QueryString["gduid"];
//给页面填充数据
load(gduid);
//输出到一个word文档中
string sPrjName = "设计书-" + Title_TBx.Text ;
Response.Buffer= true;
Response.Charset="utf-8";
//下面这行很重要,  attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
Response.AppendHeader("Content-Disposition","attachment;filename="+sPrjName+".doc");
Response.C />
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-word";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
//关闭本页面
Response.Write("window.close();");
Response.End();
}
}
这段代码可以正常运行,但是每次我点击那个输出word后的保存按钮后又会再执行一次那个弹出窗体的PageLoad事件,导致那个弹出窗体未能关闭(但是这时这个页面变成了一个空白页,原本的控件和填充的数据全都不见)。
我想请教一下怎么做才能让那个弹出窗体页面输出到word之后能够自动的正常关闭呢?
另外当输出到word之后原来页面上的表格都没有了,请问如何才能保留原来的页面布局?


解决方案1:


private void Page_Load(object sender, System.EventArgs e) 

    // 在此处放置用户代码以初始化页面 
    if (!IsPostBack) 
    { 
        WriteToWord(Request.QueryString["gduid"]);
        //关闭本页面 
        Response.Write(" window.close(); "); 
    }
}
private void WriteToWord(g)
{
    //接收页面传递的的参数 
    string gduid = g; 
    //给页面填充数据 
    load(gduid); 
    //输出到一个word文档中 
    string sPrjName = "设计书-" + Title_TBx.Text ; 
    Response.Buffer= true; 
    Response.Charset="utf-8"; 
    //下面这行很重要,  attachment 参数表示作为附件下载,您可以改成 online在线打开 
    //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc | | .xls | | .txt | |.htm 
    Response.AppendHeader("Content-Disposition","attachment;filename="+sPrjName+".doc"); 
    Response.C />
    //Response.ContentType指定文件类型 可以为application/ms-excel | | application/ms-word | | application/ms-txt | | application/ms-html | | 或其他浏览器可直接支持文档 
    Response.ContentType = "application/ms-word"; 
    this.EnableViewState = false; 
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
    this.RenderControl(oHtmlTextWriter); 
    //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件 
    Response.Write(oStringWriter.ToString()); 
    //关闭本页面 
    Response.Write(" window.close(); "); 
    Response.End(); 
}
解决方案2:

Response.Write(" window.open('MappingToWord.aspx?gduid="+gduid+"') "); 
===不用写样.写个链接就可.
如:
Response.Redirect("MappingToWord.aspx?gduid="+gduid);

上一篇关于EXCEL导入导出
下一篇aspnet中的login控件非要用它里面那个数据库吗?
明星图片
相关文章
《u盘无法弹出 无法关闭弹出窗体以及输出到word时保留表格》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)