Hello friend,

i have some question here, how can i read text by line in text.txt and put it in the text box in asp.net?

Recommended Answers

All 4 Replies

Try using the File.ReadAllText() method. It opens a text file, reads all of the lines, then closes the file. You can simply store the results in a string and display the results in a multiline textbox.

Example..

ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Demo</title>
</head>

<body>
    <form id="form1" runat="server">
        <div>
            <asp:button id="btn1" runat="server" text="Read" onclick="btn_Click" /><br /><br />
            Results:<br />
            <asp:TextBox ID="txtBox1" runat="server" TextMode="MultiLine" Height="250px"></asp:TextBox>
        </div>
    </form>
</body>
</html>
C#
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("~/TextFile.txt"));
        txtBox1.Text = txt;
    }
}
TextFile.txt

Line1
Line2
Line3
Line4
Line5

Results

dea44b8702104a82bb6de94b5912cd36

nice!!, but i mean i want to fill textbox1 with line1 and textbox2 with line2....can you help me?

Do some research on streamreader and the readline() method.

There are other alternatives.

Are you planning to have a predefined number of lines to read or are you planning to create textboxes dynamically?

i think im planing to create textboxes dynamically, because i dont know how much line in the text.txt...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.