first, the textBox tbISBN has a value that is read from the text file, but then after pressing the "Clear" button, it should be realized, and when I enter the new text into the textBox it should get the new text that I entered, but it keeps it's first value, even after the "Clear" button event.
Here is the code.
Expand|Select|Wrap|Line Numbers
- public partial class _Default : System.Web.UI.Page
- {
- private Book selectedBook;
- public string strISBN;
- public string strCurrent;
- public string[] strFields;
- public char[] chrDelimiter = { ',' };
- public SortedList slBook;
- public StreamReader srdCurrent;
- public string content;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- SortedList slBooks = new SortedList();
- StreamReader srdCurrent = new StreamReader(Request.MapPath(@"App_Data\Books.txt"));
- strCurrent = srdCurrent.ReadLine();
- while (strCurrent != null)
- {
- Book recNew = new Book();
- strFields = strCurrent.Split(chrDelimiter);
- recNew.Title = strFields[0];
- recNew.ISBN = strFields[1];
- recNew.Author = strFields[2];
- recNew.Publisher = strFields[3];
- recNew.UnitPrice = System.Convert.ToDecimal(strFields[4]);
- slBooks.Add(recNew.Title, recNew);
- strCurrent = srdCurrent.ReadLine();
- // Add the book title to the drop down list
- ddlBooks.Items.Add(recNew.Title);
- }
- // Close the file when reading is complete.
- srdCurrent.Close();
- // Add the sorted list to this session
- Session.Add("Book", slBooks);
- }
- selectedBook = GetSelectedBook();
- if (selectedBook != null)
- {
- tbTitle.Text = selectedBook.Title;
- tbISBN.Text = selectedBook.ISBN;
- tbAuthor.Text = selectedBook.Author;
- tbPublisher.Text = selectedBook.Publisher;
- tbPrice.Text = selectedBook.UnitPrice.ToString("c");
- imgBook.ImageUrl = "Images/Books/" + selectedBook.ISBN + ".gif";
- }
- }
- private Book GetSelectedBook()
- {
- SortedList slBooks = (SortedList)Session["Book"];
- if (slBooks == null)
- return null;
- string strTitle = ddlBooks.SelectedValue;
- return (Book)slBooks[strTitle];
- }
- //***********************************************************************************
- public void btnClear_Click(object sender, EventArgs e)
- {
- /*selectedBook.Title = "";
- selectedBook.ISBN = "";
- selectedBook.Author = "";
- selectedBook.Publisher = "";
- selectedBook.UnitPrice = 0.00m;*/
- StreamWriter delete = new StreamWriter(Request.MapPath(@"App_Data\Boo.txt"));
- tbTitle.Text = "";
- tbISBN.Text = "";
- tbAuthor.Text = "";
- tbPublisher.Text = "";
- tbPrice.Text = "";
- imgBook.ImageUrl = "";
- lblUploadResult.Text = "";
- }
- //***********************************************************************************
- protected void AddBook()
- {
- strISBN = tbISBN.Text;
- string filePath = Server.MapPath(@"App_Data\Books.txt");
- StreamReader sr = File.OpenText(filePath);
- //StreamReader srdCurrent = new StreamReader(Request.MapPath(@"App_Data\Books.txt"));
- //StreamWriter rite = new StreamWriter(Request.MapPath(@"App_Data\Boo.txt"));
- //rite.WriteLine("any string");
- //rite.Close();
- try
- {
- content = sr.ReadToEnd();
- if (content.Contains(strISBN))
- {
- lblUploadResult.Text = "isbn contained";
- sr.Close();
- }
- else
- {
- StreamReader srdCurrent = new StreamReader(Request.MapPath(@"App_Data\Books.txt"));
- StreamWriter rite = new StreamWriter(Request.MapPath(@"App_Data\Boo.txt"));
- rite.WriteLine("any string");
- rite.Close();
- }
- }
- catch (Exception) { }
- }
- //**************************************************************************************
- protected void btnSave_Click(object sender, EventArgs e)
- {
- AddBook();
- }
- }