Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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# 如何打开新窗口并过滤gridview_C#_Asp.net - Fatal编程技术网

C# 如何打开新窗口并过滤gridview

C# 如何打开新窗口并过滤gridview,c#,asp.net,C#,Asp.net,在新选项卡或新窗口中打开pdf文件的编码有问题。然后,我在过滤gridview时也遇到了问题。这是因为过滤gridview时,下载按钮无法工作,pdf无法下载。这是我的aspx编码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewDocument.aspx.cs" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHol

在新选项卡或新窗口中打开pdf文件的编码有问题。然后,我在过滤gridview时也遇到了问题。这是因为过滤gridview时,下载按钮无法工作,pdf无法下载。这是我的aspx编码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewDocument.aspx.cs" Title="Untitled Page" %>

  <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
   </asp:Content>
  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<center>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" CssClass="title" Text="DOCUMENT LIST"></asp:Label>
    <br />
    <br />
    <br />
    <table>
        <tr>
            <td style="text-align: center; padding-left: 25px; padding-right: 25px">
            </td>
            <td style="text-align: center">
                <asp:Button ID="Button1" runat="server" CssClass="button" Text="Contract(Test)" OnClick="Button1_Click" />
            </td>
            <td style="text-align: center; padding-left: 25px; padding-right: 25px">
            </td>
        </tr>
    </table>
    <br />
    <br />
</center>
<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="DocView" runat="server">
        <center>
            <br />
            <br />
            <table>
                <tr>
                    <td style="text-align: center; padding-left: 10px; padding-right: 15px">
                        <asp:Label ID="Label2" runat="server" Text="Search By :"></asp:Label>
                    </td>
                   <%-- <td style="text-align: center; padding-left: 5px; padding-right: 5px">
                        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropDownMenu">
                            <asp:ListItem Value="Document Name"></asp:ListItem>
                            <asp:ListItem Value="Date"></asp:ListItem>
                        </asp:DropDownList>
                    </td>--%>
                    <td style="padding-left: 5px; padding-right: 5px">
                        <asp:TextBox ID="DocSearch" runat="server" CssClass="text-input" Width="200px" MaxLength="100"
                            Font-Names="verdana"></asp:TextBox>
                    </td>
                    <td>
                        <asp:Button ID="Button2" runat="server" CssClass="button" Text="Search" OnCommand="Button2_Command" />
                    </td>
                </tr>
            </table>
            <br />
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderStyle="Solid"
                BorderColor="#ccc" BorderWidth="2px" GridLines="Horizontal" Width="650px" AutoGenerateColumns="false"
                CellPadding="3" OnRowCommand="ActionCommand" AllowSorting="true">
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <Columns>
                <asp:BoundField DataField="folderName">
                        <ItemStyle CssClass="leftAligment" />
                    </asp:BoundField>
                    <asp:BoundField DataField="fileName" HeaderText="DOCUMENT NAME">
                        <ItemStyle CssClass="leftAligment" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Date" HeaderText="DATE / TIME">
                        <ItemStyle CssClass="centerAlign" />
                    </asp:BoundField>
                    <asp:ButtonField CommandName="Open" Text="Open" ButtonType="Link" />
                    <asp:ButtonField CommandName="Download" Text="Download" />
                </Columns>
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" />
            </asp:GridView>
        </center>
        <br />
        <br />
        <br />
    </asp:View>
</asp:MultiView>
















我的aspx.cs编码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.IO;
using System.Threading;
using System.Net;
using System.Xml;

namespace Hibah_Total_v1._2.Secure
{
public partial class ViewDocument : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.Visible = false;
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.Visible = true;
        MultiView1.SetActiveView(DocView);
        string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
        string path = Server.MapPath("~") + "/Secure/";
        string fullPath = path + folderName;
        string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");
        DataTable table = GetTable(filePaths);

        GridView1.Columns[0].Visible = true;
        GridView1.DataSource = table;
        GridView1.DataBind();
        GridView1.Columns[0].Visible = false;

    }


    public DataTable GetTable(string[] filePaths)
    {
        DataTable table = new DataTable();
        table.Columns.Add("folderName", typeof(string));
        table.Columns.Add("fileName", typeof(string));
        table.Columns.Add("Date", typeof(string));

        DataRow row;
        {
            for (int i = 0; i < filePaths.Length; i++)
            {
                row = table.NewRow();


                row["folderName"] = filePaths[i];
                FileInfo Myfile = new FileInfo(filePaths[i]);
                row["fileName"] = Myfile.Name.ToString();
                row["Date"] = Myfile.CreationTime.ToString();

                table.Rows.Add(row);

            }
        }


        Session["Table"] = table;

        this.GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
        this.GridView1.DataBind();

        return table;

    }





    public void ActionCommand(object sender, GridViewCommandEventArgs e)
    {
        string commandName = e.CommandName.ToString().Trim();
        GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
        string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
        string path = Server.MapPath("~") + "/Secure/";
        string fullPath = path + folderName;
        string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");            
            switch (commandName)
            {
                case "Open":

                    ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('OpenForm.aspx?path=" + row.Cells[0].Text + "');", true);
                    break;
                case "Download":

                    FileStream fs = new FileStream(row.Cells[0].Text, FileMode.Open, FileAccess.Read);
                    byte[] ar = new byte[(int)fs.Length];
                    fs.Read(ar, 0, (int)fs.Length);
                    fs.Close();

                    Response.AddHeader("content-disposition", "attachment;fileName=" + HttpUtility.UrlEncode(row.Cells[1].Text, System.Text.Encoding.UTF8));
                    Response.ContentType = "application/octectstream";
                    Response.BinaryWrite(ar);
                    Response.End();
                    break;
                default: break;
            }

    }


    protected void Button2_Command(object sender, EventArgs e)
    {
        string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
        string path = Server.MapPath("~") + "/Secure/";
        string fullPath = path + folderName;
        string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");

        if (DocSearch.Text.ToString().Trim() != "")
        {
            DataTable dt = (DataTable)Session["Table"];
            var query = from t in dt.AsEnumerable() where t.Field<string>("folderName").StartsWith(DocSearch.Text.ToString().Trim()) || t.Field<string>("folderName").Contains(DocSearch.Text.ToString().Trim()) select t;

            DataTable dtable = new DataTable();
            dtable = query.CopyToDataTable();
            this.GridView1.DataSource = dtable;
            this.GridView1.DataBind();

        }
        else
        {
            this.GridView1.DataSource = ((DataTable)Session["table"]).DefaultView;
            this.GridView1.DataBind();
        }

    }



  }
}
使用系统;
使用系统集合;
使用系统配置;
使用系统数据;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
使用System.Xml.Linq;
使用System.IO;
使用系统线程;
Net系统;
使用System.Xml;
命名空间Hibah_Total_v1._2.Secure
{
公共部分类ViewDocument:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
MultiView1.Visible=false;
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
MultiView1.Visible=true;
MultiView1.SetActiveView(DocView);
string folderName=ConfigurationManager.AppSettings[“folderPDF”].ToString();
字符串路径=Server.MapPath(“~”+”/Secure/”;
字符串fullPath=path+folderName;
字符串[]filepath=Directory.GetFiles(fullPath,*.pdf”);
DataTable=GetTable(文件路径);
GridView1.Columns[0]。Visible=true;
GridView1.DataSource=表格;
GridView1.DataBind();
GridView1.Columns[0]。Visible=false;
}
公共数据表可获取(字符串[]文件路径)
{
DataTable=新的DataTable();
table.Columns.Add(“folderName”,typeof(string));
table.Columns.Add(“文件名”,typeof(字符串));
表.列.添加(“日期”,类型(字符串));
数据行;
{
for(int i=0;i
有人能帮我吗
       private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}