如何将txt文件内容添加到文本框
how to add txt file contents to textbox
我收到错误消息 'TextBox1 does not exist in this context'
我是 c# 的新手,完全不知道这个问题。
正如您在我的代码中看到的那样,我想使用文本框而不是所有标签。
目前我正在测试本地主机;不确定这是否有任何区别。
我的想法是在 Formview 创建控件之前执行代码文件。
我的 ASPX 文件
<%@ Page language="C#" masterpagefile="WebBuilder.master" CodeFile="~/Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Bodyhead" ContentPlaceHolderID="BodyHead" runat="server">
<div align="center" style="background-position: center; background-image: url('Images/Rishton-waterfall-Edited.png'); height: 235px; background-repeat: no-repeat; background-color: #FFFFFF; top: 10px;">
</div>
</asp:Content>
<asp:Content ID="NavTop" ContentPlaceHolderID="NavTop" runat="server">
<p align="center" style="font-weight: bold"> <%:DateTime.Now.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'") %></p>
<div align="center">
<asp:Menu ID="Menu3"
runat="server"
Orientation="Horizontal"
RenderingMode="Table"
BackColor="Blue"
ForeColor="Yellow"
Font-Size="14pt"
Width="100%" >
<DynamicHoverStyle ForeColor="Black" Font-Size="16pt" />
<DynamicMenuItemStyle ForeColor="Black" />
<Items>
<asp:MenuItem Text="Home" Value="Home" NavigateUrl="~/Default.aspx"></asp:MenuItem>
<asp:MenuItem Text="Rishton History" Value="Rishton History" NavigateUrl="~/Default.aspx?Page=History"></asp:MenuItem>
<asp:MenuItem Text="Living in Rishton" Value="Living in Rishton" NavigateUrl="">
<asp:MenuItem Text="Streets" Value="Streets" ToolTip="All the streets found in Rishton">
<asp:MenuItem Text="Spring St" Value="Spring St" NavigateUrl="Default.aspx?Page=Spring"></asp:MenuItem>
<asp:MenuItem Text="Livesey St" Value="Livesey St" NavigateUrl="Default.aspx?Page=Livesey"></asp:MenuItem>
<asp:MenuItem Text="High St" Value="High St" NavigateUrl="Default.aspx?Page=High"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Weather" Value="Weather" NavigateUrl=""></asp:MenuItem>
</Items>
<StaticHoverStyle Font-Size="16pt" ForeColor="Black" />
</asp:Menu>
</div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" SiteMapProvider="MySiteMap" />
<asp:Menu
ID="Menu2" runat="server" CssClass="menu" Visible="false"
EnableViewState="false" IncludeStyleBlock="false" StaticDisplayLevels="2"
Orientation="Horizontal" DataSourceID="SiteMapDataSource1" >
</asp:Menu>
<asp:SiteMapPath ID="SiteMapPath1" runat="server" SiteMapProvider="MySiteMap" Font-Names="Verdana" Font-Size="14pt" PathSeparator=" > ">
<CurrentNodeStyle ForeColor="Black" />
<NodeStyle Font-Bold="True" ForeColor="Blue" />
<PathSeparatorStyle Font-Bold="True" ForeColor="Red" />
<RootNodeStyle Font-Bold="True" ForeColor="Red" />
</asp:SiteMapPath>
<p align="center" style="border-style: outset">Interactive Website Information Bar</p>
</asp:Content>
<asp:Content ID="Content" ContentPlaceHolderID="Content" runat="server">
<h1 align="center">Rishton</h1>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [AllSite] WHERE ([Keyword] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Keyword" QueryStringField="Page" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView runat="server" id="FormView1" DataKeyNames="ID" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<h2><asp:Label id="TitleLabel" runat="server" Text='<%# Bind("Title") %>' /></h2>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server" Text="Test" ></asp:TextBox>
<br />
<asp:Label id="Para1Label" runat="server" Text='<%# Bind("Para1") %>' />
<br /> <br />
<asp:Label id="Label1" runat="server" Text='<%# Bind("Para2") %>' />
<br /> <br />
<asp:Label id="Label2" runat="server" Text='<%# Bind("Para3") %>' />
<br /> <br />
<asp:Label id="Label3" runat="server" Text='<%# Bind("Para4") %>' />
<br /> <br />
<asp:Label id="Label4" runat="server" Text='<%# Bind("Para5") %>' />
<br /> <br />
<asp:Label id="Label5" runat="server" Text='<%# Bind("Para6") %>' />
<br /> <br />
<asp:Label id="Label6" runat="server" Text='<%# Bind("Para7") %>' />
<br /> <br />
<asp:Label id="Label7" runat="server" Text='<%# Bind("Para8") %>' />
<br /> <br />
<asp:Label id="Label10" runat="server" Text='<%# Bind("Para9") %>' />
<br /> <br />
<asp:Label id="Label11" runat="server" Text='<%# Bind("Para10") %>' />
<br /> <br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("ImageLink") %>' Width="640px" Height="320px" />
<br /> <br />
</ItemTemplate>
</asp:FormView>
</asp:Content>
<asp:Content ID="Footer" ContentPlaceHolderID="Foot" runat="server">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [AllSite] WHERE ([Keyword] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Keyword" QueryStringField="Page" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<h1>References</h1>
<asp:FormView runat="server" id="FormView2" DataKeyNames="ID" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:Label id="Refs1" runat="server" Text='<%# Bind("Refs1") %>' />
<br /> <br />
<asp:Label id="Refs2" runat="server" Text='<%# Bind("Refs2") %>' />
<br /> <br />
<asp:Label id="Refs3" runat="server" Text='<%# Bind("Refs3") %>' />
<br /> <br />
<asp:Label id="Refs4" runat="server" Text='<%# Bind("Refs4") %>' />
<br /> <br />
<asp:Label id="Refs5" runat="server" Text='<%# Bind("Refs5") %>' />
<br /> <br />
<asp:Label id="Refs6" runat="server" Text='<%# Bind("Refs6") %>' />
<br /> <br />
<asp:Label id="Refs7" runat="server" Text='<%# Bind("Refs7") %>' />
<br /> <br />
<asp:Label id="Refs8" runat="server" Text='<%# Bind("Refs8") %>' />
<br /> <br />
<asp:Label id="Refs9" runat="server" Text='<%# Bind("Refs9") %>' />
<br /> <br />
<asp:Label id="Refs10" runat="server" Text='<%# Bind("Refs10") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<p align="center">© <%: DateTime.Now.Year %> - Website Builder Ver 1.0 </p>
</asp:Content>
我的aspx.cs文件
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void btn_Click(object sender, EventArgs e,)
{
string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
TextBox1 = txt;
}
}
在此先感谢您的帮助。
您应该先访问 FormView1,然后再访问其名为 TextBox1 的子项。你可以这样访问
string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
//TextBox1 = txt;
TextBox textBox1 = ((TextBox)FormView1.FindControl("TextBox1"));
textBox1.Text = txt;
无论您是在本地主机还是在服务器上。
我收到错误消息 'TextBox1 does not exist in this context'
我是 c# 的新手,完全不知道这个问题。 正如您在我的代码中看到的那样,我想使用文本框而不是所有标签。
目前我正在测试本地主机;不确定这是否有任何区别。
我的想法是在 Formview 创建控件之前执行代码文件。
我的 ASPX 文件
<%@ Page language="C#" masterpagefile="WebBuilder.master" CodeFile="~/Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Bodyhead" ContentPlaceHolderID="BodyHead" runat="server">
<div align="center" style="background-position: center; background-image: url('Images/Rishton-waterfall-Edited.png'); height: 235px; background-repeat: no-repeat; background-color: #FFFFFF; top: 10px;">
</div>
</asp:Content>
<asp:Content ID="NavTop" ContentPlaceHolderID="NavTop" runat="server">
<p align="center" style="font-weight: bold"> <%:DateTime.Now.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'") %></p>
<div align="center">
<asp:Menu ID="Menu3"
runat="server"
Orientation="Horizontal"
RenderingMode="Table"
BackColor="Blue"
ForeColor="Yellow"
Font-Size="14pt"
Width="100%" >
<DynamicHoverStyle ForeColor="Black" Font-Size="16pt" />
<DynamicMenuItemStyle ForeColor="Black" />
<Items>
<asp:MenuItem Text="Home" Value="Home" NavigateUrl="~/Default.aspx"></asp:MenuItem>
<asp:MenuItem Text="Rishton History" Value="Rishton History" NavigateUrl="~/Default.aspx?Page=History"></asp:MenuItem>
<asp:MenuItem Text="Living in Rishton" Value="Living in Rishton" NavigateUrl="">
<asp:MenuItem Text="Streets" Value="Streets" ToolTip="All the streets found in Rishton">
<asp:MenuItem Text="Spring St" Value="Spring St" NavigateUrl="Default.aspx?Page=Spring"></asp:MenuItem>
<asp:MenuItem Text="Livesey St" Value="Livesey St" NavigateUrl="Default.aspx?Page=Livesey"></asp:MenuItem>
<asp:MenuItem Text="High St" Value="High St" NavigateUrl="Default.aspx?Page=High"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Weather" Value="Weather" NavigateUrl=""></asp:MenuItem>
</Items>
<StaticHoverStyle Font-Size="16pt" ForeColor="Black" />
</asp:Menu>
</div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" SiteMapProvider="MySiteMap" />
<asp:Menu
ID="Menu2" runat="server" CssClass="menu" Visible="false"
EnableViewState="false" IncludeStyleBlock="false" StaticDisplayLevels="2"
Orientation="Horizontal" DataSourceID="SiteMapDataSource1" >
</asp:Menu>
<asp:SiteMapPath ID="SiteMapPath1" runat="server" SiteMapProvider="MySiteMap" Font-Names="Verdana" Font-Size="14pt" PathSeparator=" > ">
<CurrentNodeStyle ForeColor="Black" />
<NodeStyle Font-Bold="True" ForeColor="Blue" />
<PathSeparatorStyle Font-Bold="True" ForeColor="Red" />
<RootNodeStyle Font-Bold="True" ForeColor="Red" />
</asp:SiteMapPath>
<p align="center" style="border-style: outset">Interactive Website Information Bar</p>
</asp:Content>
<asp:Content ID="Content" ContentPlaceHolderID="Content" runat="server">
<h1 align="center">Rishton</h1>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [AllSite] WHERE ([Keyword] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Keyword" QueryStringField="Page" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView runat="server" id="FormView1" DataKeyNames="ID" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<h2><asp:Label id="TitleLabel" runat="server" Text='<%# Bind("Title") %>' /></h2>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server" Text="Test" ></asp:TextBox>
<br />
<asp:Label id="Para1Label" runat="server" Text='<%# Bind("Para1") %>' />
<br /> <br />
<asp:Label id="Label1" runat="server" Text='<%# Bind("Para2") %>' />
<br /> <br />
<asp:Label id="Label2" runat="server" Text='<%# Bind("Para3") %>' />
<br /> <br />
<asp:Label id="Label3" runat="server" Text='<%# Bind("Para4") %>' />
<br /> <br />
<asp:Label id="Label4" runat="server" Text='<%# Bind("Para5") %>' />
<br /> <br />
<asp:Label id="Label5" runat="server" Text='<%# Bind("Para6") %>' />
<br /> <br />
<asp:Label id="Label6" runat="server" Text='<%# Bind("Para7") %>' />
<br /> <br />
<asp:Label id="Label7" runat="server" Text='<%# Bind("Para8") %>' />
<br /> <br />
<asp:Label id="Label10" runat="server" Text='<%# Bind("Para9") %>' />
<br /> <br />
<asp:Label id="Label11" runat="server" Text='<%# Bind("Para10") %>' />
<br /> <br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("ImageLink") %>' Width="640px" Height="320px" />
<br /> <br />
</ItemTemplate>
</asp:FormView>
</asp:Content>
<asp:Content ID="Footer" ContentPlaceHolderID="Foot" runat="server">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [AllSite] WHERE ([Keyword] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Keyword" QueryStringField="Page" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<h1>References</h1>
<asp:FormView runat="server" id="FormView2" DataKeyNames="ID" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:Label id="Refs1" runat="server" Text='<%# Bind("Refs1") %>' />
<br /> <br />
<asp:Label id="Refs2" runat="server" Text='<%# Bind("Refs2") %>' />
<br /> <br />
<asp:Label id="Refs3" runat="server" Text='<%# Bind("Refs3") %>' />
<br /> <br />
<asp:Label id="Refs4" runat="server" Text='<%# Bind("Refs4") %>' />
<br /> <br />
<asp:Label id="Refs5" runat="server" Text='<%# Bind("Refs5") %>' />
<br /> <br />
<asp:Label id="Refs6" runat="server" Text='<%# Bind("Refs6") %>' />
<br /> <br />
<asp:Label id="Refs7" runat="server" Text='<%# Bind("Refs7") %>' />
<br /> <br />
<asp:Label id="Refs8" runat="server" Text='<%# Bind("Refs8") %>' />
<br /> <br />
<asp:Label id="Refs9" runat="server" Text='<%# Bind("Refs9") %>' />
<br /> <br />
<asp:Label id="Refs10" runat="server" Text='<%# Bind("Refs10") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<p align="center">© <%: DateTime.Now.Year %> - Website Builder Ver 1.0 </p>
</asp:Content>
我的aspx.cs文件
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void btn_Click(object sender, EventArgs e,)
{
string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
TextBox1 = txt;
}
}
在此先感谢您的帮助。
您应该先访问 FormView1,然后再访问其名为 TextBox1 的子项。你可以这样访问
string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
//TextBox1 = txt;
TextBox textBox1 = ((TextBox)FormView1.FindControl("TextBox1"));
textBox1.Text = txt;
无论您是在本地主机还是在服务器上。