无法在网格视图中打印文件名

Not able to print the filenames in a gridview

我正在尝试在 ASP.NET 中创建一个页面,它允许我查看我的文件目录中所有文件的列表。我只想显示文件名并允许用户稍后编辑文件。 我在这里尝试过这种方法来获取所有文件名并制作数据表,然后将其绑定到 gridview。这是我的代码。

protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataRow row; 
            dt.Columns.Add("FileNames",typeof(string));
            string path = Server.MapPath("~/Files");
            DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo fi in di.GetFiles()) {
                row = dt.NewRow();
                row["FileNames"] = fi.ToString();
                dt.Rows.Add(row);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
            }

我不明白这里的问题是什么。当我 运行 页面时,我看到一个空的 GridView,其中只有一个 header 行,标题为 FileName 但没有文件名。我已经检查过我的“文件”文件夹中有文件并使用 response.write() 我也能够打印这些名称但无法在网格视图中显示它。

这是我的 Gridview 代码

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
           <Columns>
               <asp:TemplateField HeaderText="FileNames">
                   <EditItemTemplate>
                       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                   </EditItemTemplate>
                   <ItemTemplate>
                       <asp:Label ID="Label1"  runat="server"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>

帮助我理解这种方法并告诉我如何在 gridview 中打印名称。

你需要给字段名来绑定控件,像这样-

<EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FileName") %>'></asp:TextBox>
</EditItemTemplate>

您有两个问题

首先是这个:

fi.ToString();

但是,“fi”是一种文件信息类型。您需要 use/pull 文件名,如下所示:

fi.Name (for file name)
fi.FullName (for full file name)

而且,您必须像其他张贴者指出的那样使用 Eval() 表达式。

您可以让网格自动创建列。因此,使用此标记:

<div style="width:45%">
  <asp:GridView ID="GridView1" runat="server" CssClass="table table-hover">
  </asp:GridView>
</div>

然后说这个代码:

   protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack) {
            LoadFiles();
        }
    }


    public void LoadFiles()
    {
        MyTable.Columns.Add("Date", typeof(string));
        MyTable.Columns.Add("File Size", typeof(string));
        MyTable.Columns.Add("File Name", typeof(string));
        MyTable.Columns.Add("FullFile", typeof(string));

        var strFolder = Server.MapPath("~/Scripts");

        DirectoryInfo MyDir = new DirectoryInfo(strFolder);
        FileInfo[] MyFiles = MyDir.GetFiles("*.*");

        foreach (FileInfo MyFile in MyFiles)
        {
            // Dim oneRow As DataRow = MyTable.Rows.Add
            DataRow oneRow;
            oneRow = MyTable.NewRow();

            oneRow["Date"] = MyFile.LastAccessTime.ToShortDateString();
            oneRow["File Size"] = (int)(MyFile.Length / (double)1024) + " KB";
            oneRow["File Name"] = MyFile.Name;
            oneRow["FullFile"] = MyFile.FullName;

            MyTable.Rows.Add(oneRow);
        }

        GridView1.DataSource = MyTable;
        GridView1.DataBind();
    }

输出:

如果你想在gridview上放置(有控件),那么你当然可以使用模板字段。您可以像这样使用数据绑定字段:

注意 AutoGeneratecolumns = false 现在

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="table table-hover">
   <Columns>
     <asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="100px" />
     <asp:BoundField DataField="File Name" HeaderText="ID" />
     <asp:BoundField DataField="FullFile" HeaderText="ID" />
     <asp:BoundField DataField="File Size" HeaderText="Size" ItemStyle-Width="100px" />
   </Columns>
  </asp:GridView>

输出与第一个示例非常相似。

在上面添加一个按钮,我们可以点击一行,获取信息。

所以,我们的标记变成了这样说。如果我们自动生成,我们实际上不必添加列,但让我们这样做,这样我们就可以控制控件的放置(例如我们的视图按钮)。

所以,现在这里是完整的标记和使用的代码:

       <div style="width:45%">

            <asp:GridView ID="GridView1" runat="server" CssClass="table table-hover" AutoGenerateColumns="false">

                <Columns>
                <asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="100px" />
                <asp:BoundField DataField="File Name" HeaderText="File Name" />
                <asp:BoundField DataField="FullFile" HeaderText="Full File" />
                <asp:BoundField DataField="File Size" HeaderText="Size" ItemStyle-Width="100px" />

                    <asp:TemplateField HeaderText="View">
                        <ItemTemplate>
                            <asp:Button ID="cmdView" runat="server" Text="View"
                                CommandName="Select"
                                OnClick="cmdView_Click"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
             </asp:GridView>

        </div>

现在我们的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            LoadFiles();
        }
    }

    public void LoadFiles()
    {
        DataTable MyTable = new DataTable();

        MyTable.Columns.Add("Date", typeof(string));
        MyTable.Columns.Add("File Size", typeof(string));
        MyTable.Columns.Add("File Name", typeof(string));
        MyTable.Columns.Add("FullFile", typeof(string));

        var strFolder = Server.MapPath("~/Scripts");

        DirectoryInfo MyDir = new DirectoryInfo(strFolder);
        FileInfo[] MyFiles = MyDir.GetFiles("*.*");

        foreach (FileInfo MyFile in MyFiles)
        {
            // Dim oneRow As DataRow = MyTable.Rows.Add
            DataRow oneRow;
            oneRow = MyTable.NewRow();

            oneRow["Date"] = MyFile.LastAccessTime.ToShortDateString();
            oneRow["File Size"] = (int)(MyFile.Length / (double)1024) + " KB";
            oneRow["File Name"] = MyFile.Name;
            oneRow["FullFile"] = MyFile.FullName;

            MyTable.Rows.Add(oneRow);
        }

        GridView1.DataSource = MyTable;
        GridView1.DataBind();
    }

    protected void cmdView_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow gvR = (GridViewRow)btn.Parent.Parent;

        Response.Write("<h2> File name selected = " + gvR.Cells[2].Text + "</h2>");

    }

现在我们的输出:

并注意我们现在如何单击任何行。我点击了此屏幕截图的第二行:

请注意单击一下即可获取网格行的绝妙技巧 - 不必使用数据项索引更改和所有这些废话。

只需将 asp .net 按钮拖放到网格视图(在模板内),然后像我一样添加点击事件。

仅供参考:请注意,您必须包含 CommandName="Select"。这将导致行索引位置发生变化 - 如果您不包括它,那么我们简单的按钮单击将不会 get/see 当前选择的网格行。所以,不要忘记所有重要的命令。请注意,使用 CommandName="Select" 也会触发 GV 索引更改事件,但这种方式我们只使用简单的按钮事件 - 因此不太在意。

编辑------------------------

关于我使用 btn.parent.Parent 获取所选网格行的一些问题。问题是我只是想使用按钮单击事件,但我们没有必要。

以上仍然有效,但我建议删除按钮单击事件存根,并将代码放在此处:

   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvR = GridView1.SelectedRow;

        Response.Write("<h2> File name selected = " + gvR.Cells[2].Text + "</h2>");

    }