'_____________________________________________________________- ' Introduction to Stream Reader in ASP.Net ' Ron Kessler ' Created 9/2/05 '_____________________________________________________________ 'Updated 3/24/08 'Updates: ' Updated to VS2005. This particular form is still using the VS2003 style so notice the ' Windows generated code section in this form. 'NOTES: 'If you want to listbox to work like you expect, then set the autopostback property=true 'features include, ' How to make a message box on a web form ' How to iterate through the controls collection ' How to use stream reader ' Introduction to the IsPostBack property ' How to set the root path for the application ' Reminder about formatting currency '==================START OF CODE====================== Imports System.IO 'this adds streamreader class Namespace LearningASP Partial Class Streams Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Const TAXRATE = 0.075 Dim rootPath As String = Server.MapPath("~") Dim FileName As String = rootPath & ("\items.txt") Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then Me.btnReturn.Attributes.Add("onclick", "return confirm('Are you sure you want to return?');") End If txtItem.Style("text-align") = "right" txtPrice.Style("text-align") = "right" txtQuantity.Style("text-align") = "right" txtSubTotal.Style("text-align") = "right" txtTax.Style("text-align") = "right" txtTotal.Style("text-align") = "right" GetItems() End Sub Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) GetItems() End Sub Private Sub GetItems() '---we only want to read our data once so make sure it is inside the IsPostBack stuff If Not IsPostBack Then Dim MyReader As New StreamReader(FileName) Dim ItemInFile As String Dim Price As String Dim Product As String Try Do ItemInFile = MyReader.ReadLine() 'actual text in the file Product = Trim(Left(ItemInFile, 10)) 'the first 10 characters are the item Price = Trim(Mid(ItemInFile, 20)) 'starting at 20 we have the prices '---add the product & price Product is text and price is the value. See items collection in the 'properties window for the listbox lstItems.Items.Add(New ListItem(Product, Price)) Loop Until ItemInFile = Nothing Catch lblError.Text = "I can't read the data file." lblError.Visible = True Finally MyReader.Close() End Try End If End Sub Private Sub lstItems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstItems.SelectedIndexChanged '---once the text property contains the item and the value property contains the price, grab them! txtItem.Text = lstItems.SelectedItem.Text txtPrice.Text = lstItems.SelectedValue End Sub Private Sub btnCheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckOut.Click Try lblError.Visible = False '---compute sub total txtSubTotal.Text = FormatNumber(txtPrice.Text * txtQuantity.Text) '---compute tax txtTax.Text = FormatNumber(txtSubTotal.Text * TAXRATE) '---amount due txtTotal.Text = FormatNumber(Convert.ToDecimal(txtSubTotal.Text) + Convert.ToDecimal(txtTax.Text)) Catch lblError.Text = "Please make sure all data is entered correctly...." lblError.Visible = True End Try End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click '---iterate through the controls collection '---define (Dim) ctrl in the loop itself like this: 'For Each ctrl As Control In Page.FindControl("Form1").Controls 'or do it the regular way... Dim ctrl As Control For Each ctrl In Page.FindControl("Form1").Controls If ctrl.GetType() Is GetType(TextBox) Then CType(ctrl, TextBox).Text = "" End If Next End Sub Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click Response.Redirect("default.aspx") End Sub End Class End Namespace