将目录信息内容加载到文本框中

本文关键字:文本 加载 信息内容 | 更新日期: 2023-09-27 18:19:47

我在学习此Microsoft教程时遇到问题(步骤2):步骤2:修复未经授权、经过身份验证的用户的工作流程

我的ROOT中有一个完整的文件列表,所以当我在网格视图中单击"编辑"时,文件的内容应该显示在文本框中。因此,假设我单击"编辑到Default.aspx",则应该显示Default.aspx中的内容/代码。

.ASPX:

 <asp:TextBox ID="FileContents" runat="server" Rows="10" TextMode="MultiLine" Width="95%"></asp:TextBox>
    <asp:GridView ID="FilesGrid" DataKeyNames="Name" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:CommandField SelectText="View" ShowSelectButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Length" DataFormatString="{0:N0}" HeaderText="Size in Bytes"
                HtmlEncode="False" />
        </Columns>
    </asp:GridView>

.ASPX.CS:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string appPath = Request.PhysicalApplicationPath;
            DirectoryInfo dirInfo = new DirectoryInfo(appPath);
            FileInfo[] files = dirInfo.GetFiles();
            FilesGrid.DataSource = files;
            FilesGrid.DataBind();
        }
    }
    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Open the file and display it
        string fullFileName = FilesGrid.SelectedValue.ToString();
        string contents = File.ReadAllText(fullFileName);
        FileContents.Text = contents;
    }

将目录信息内容加载到文本框中

您的

<asp:GridView ID="FilesGrid" DataKeyNames="Name" runat="server" AutoGenerateColumns="False">

缺少onselectedindexchanged

<asp:GridView ID="FilesGrid" DataKeyNames="Name" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="FilesGrid_SelectedIndexChanged">

你能更改这个代码吗

    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Open the file and display it
        string fullFileName = FilesGrid.SelectedValue.ToString();
        string contents = File.ReadAllText(fullFileName);
        FileContents.Text = contents;
    }

像这样阅读

    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Open the file and display it
        string fullFileName = FilesGrid.SelectedValue.ToString();
        List<string> contents = new List<string>(File.ReadAllLines(fullFileName));
        FileContents.Text = contents.ToString();
    }

*还要确保fullFileName具有完全限定的FilePath+FileName