Home > Blockchain >  how to add txt file contents to textbox
how to add txt file contents to textbox

Time:10-26

I am receiving the error 'TextBox1 does not exist in this context'

I am new to c# and totally lost as to the problem. As you can see in my code I would like to use Textbox rather than all the labels.

At present I am working on localhost while testing; not sure if this makes any difference.

My thoughts are the code file is being executed before the Formview has created the controls.

my ASPX file

<%@ 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">&copy; <%: DateTime.Now.Year %> - Website Builder Ver 1.0  </p>
</asp:Content>

my aspx.cs file

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;
    }
}

Thanks in advance for any help.

CodePudding user response:

You should access to FormView1 then its children named TextBox1. You can access like this

string txt = File.ReadAllText(Server.MapPath("~/Text/RishtonHistory.txt"));
//TextBox1 = txt;
TextBox textBox1 = ((TextBox)FormView1.FindControl("TextBox1"));
textBox1.Text = txt;

It does not matter your are on localhost or server.

  • Related