所以我设法将我的管理区域导出,以便用户下载到xml文件格式。但是我已经设置了一些过滤选项,因此登录的用户可以缩小可查看的结果范围。我试图实现的是在用户应用这些过滤器之后才能导出过滤后的数据。
以下代码:
protected void ExportData_Click(object sender, EventArgs e)
{
string consString = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
StringBuilder sb = new StringBuilder();
using (SqlConnection con = new SqlConnection(consString))
{
con.Open();
string sql = ("SELECT [ID], [HouseNumber], [PropAddress], [Town], [County], [PostCode] FROM Zoopla;");
SqlCommand cmd = new SqlCommand(sql, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataBind();
cmd.Dispose();
con.Close();
string filename = "DownloadTest.xml";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
过滤器设置如下:
Filter By Weeks:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="TortoiseDBZoopla" DataTextField="Weeks" DataValueField="Weeks">
</asp:DropDownList>
Filter By Status:
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="ZooplaProperties" DataTextField="PropStatus" DataValueField="PropStatus">
</asp:DropDownList>
答案 0 :(得分:0)
您的gridview已经过滤,但是在导出时,您正在运行没有过滤器的新SQL查询。您需要在查询中添加where子句以过滤结果,或者以某种方式保留gridview数据源(Viewstate,Session,Cache ...)并将其用作导出数据源。