当第一个页面跳入第二个时,按钮URL ?传值就会导出页面,而不是word文件这是为什么
代码  为了方便可直接粘到页面中运行,大家试试把第一个页面“不传值”和“传值”试试
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {        }
        //传值跳入
        protected void Button1_Click1(object sender, EventArgs e)
        {
            Response.Redirect("Default2.aspx?id=1");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="传值跳入" />
    
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {        }
        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
        }        //导出
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("导出文件.xls", Encoding.UTF8).ToString());
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            table1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();        }
    </script></head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1" id="table1" runat="server">
            <tr>
                <td>
                    1
                </td>
                <td>
                    2
                </td>
            </tr>
            <tr>
                <td>
                    3
                </td>
                <td>
                    4
                </td>
            </tr>
        </table>
    </div>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
    </form>
</body>
</html>

解决方案 »

  1.   

    O(∩_∩)O~,个人之见,页面本身有机制的,你在一个页面上实现功能,那没有问题,但是你把功能做到另一个页面上,然后访问那个aspx页面,肯定要先走页面吧。
      

  2.   

    没仔细看你的代码,对不起。要实现文件下载,必须使用Response.BinaryWrite写数据流或者将流写入Response.OutputStream中,或者先存为文件,使用Response.TransmitFile下载文件。附一个下载文件的通用方法:        public void DowlLoadFile(string filepath,string fileName)
            {
                if (File.Exists(filepath))
                {
                    Response.ContentType = "application/octet-stream";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
                    Response.TransmitFile(filepath);
                }
            }
      

  3.   


    楼主精神诚可贵,
    勤于思考谦好问。
    一朝误入胡同里,
    花费时间无回味。感于lz的精神,将lz的代码进行了测试,发现能正常导出为所需excel文件,并且只需Default2既可,而与传不传值无关,建议lz用下面的代码copy过去再试一下。<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
      <style type="text/css">
            .style1
            {
                width: 100%;
            }
        </style>    <script runat="server">
            protected void Page_Load(object sender, EventArgs e)
            {        }
            public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
            {
            }        //导出
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("导出文件.xls", Encoding.UTF8).ToString());
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;
                System.IO.StringWriter tw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                table1.RenderControl(hw);
                Response.Write(tw.ToString());
                Response.End();        }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table class="style1" id="table1" runat="server">
                <tr>
                    <td>
                        1
                    </td>
                    <td>
                        2
                    </td>
                </tr>
                <tr>
                    <td>
                        3
                    </td>
                    <td>
                        4
                    </td>
                </tr>
            </table>
        </div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
        </form>
    </body>
    </html>因与default无关,不再粘其代码。另外,lz的系统方面是不是有问题,可将你的代码放到别的机器上运行看结果如何。
      

  4.   


    主要是我有一个必须传值到Default2的一个页面,导出的东西就是Default2.aspx页面
      

  5.   


    我也测试过很多次。ID=1,ID=2,去掉ID=,一样可以导出同样的东西。。
      

  6.   

    我刚才试了就不行啊,怎么办呢url还是导致导出的原因