How to display text file contents which is stored in .txt format to a textbox when button is click [Resolved]

Posted by Jitenkumar under ASP.NET on 8/27/2013 | Points: 10 | Views : 1145 | Status : [Member] | Replies : 1
How to display text file contents which is stored in .txt format to a textbox when button is clicked in asp.net?




Responses

Posted by: Satyapriyanayak on: 8/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="text.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtMyFile" TextMode="MultiLine" Rows="10" Columns="60" Runat="server" ForeColor="Red" /><br />

<asp:button ID="bt1" Text="ReadText File" Runat="server" />

<asp:Label ID="lb1" Font-Bold="True" ForeColor="#FF9900" Runat="server" />
</div>
</form>
</body>
</html>


Imports System.IO
Partial Public Class WebForm1
Inherits System.Web.UI.Page


Protected Sub bt1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt1.Click
Dim sr1 As StreamReader

Try

sr1 = File.OpenText(Server.MapPath(".\test\") & "test.txt")

txtMyFile.Text = sr1.ReadToEnd()

lb1.Text = "File Read Succesfully !"

sr1.Close()

Catch err As Exception

lb1.Text = "File Read failed" & err.ToString()

Finally

End Try
End Sub
End Class


If this post helps you mark it as answer
Thanks

Jitenkumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response