the Web Form html and asp
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="ExampleWebPage.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div> <h1>Enter Quotes</h1> <p> Enter your favorite quotes <asp:TextBox ID="txtQuote" runat="server"></asp:TextBox> </p> <p> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></p> <p> <asp:Button ID="btnQuote" runat="server" Text="Get Quote" OnClick="btnQuote_Click" /> <asp:Label ID="Label1" runat="server" Text="Label" CssClass="response"></asp:Label></p> <asp:Button ID="btnGetAllQuotes" runat="server" Text="Get all Quotes" OnClick="btnGetAllQuotes_Click" /> <asp:BulletedList ID="BulletedList1" runat="server"></asp:BulletedList> </div> </form> </body> </html>
The code behind the web page
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { Quote q = new Quote(); protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { //if the session exists //get the existing instance of the class //from the session and use it if (Session["quotes"] != null) { q = (Quote)Session["quotes"]; } q.AddQuote(txtQuote.Text); Session["quotes"] = q; txtQuote.Text = ""; //clear textbox } protected void btnQuote_Click(object sender, EventArgs e) { //get the current instance saved in the session q = (Quote)Session["quotes"]; //call its method Label1.Text = q.GetQuote(); } protected void btnGetAllQuotes_Click(object sender, EventArgs e) { q = (Quote)Session["quotes"]; BulletedList1.DataSource = q.Quotes; BulletedList1.DataBind(); } }
the Quote class
using System; using System.Collections.Generic; using System.Linq; using System.Web; ////// Summary description for Quote /// public class Quote { // create a list object to store strings private Listquotes; //initialize the list in the constructor public Quote() { quotes = new List (); } //read only property public List Quotes { get { return quotes; } } //add quotes to the list public void AddQuote(string aQuote) { quotes.Add(aQuote); } //return a random quote public string GetQuote() { string quoteResult = null; Random rand = new Random(); if (quotes.Count != 0) { int number = rand.Next(0, quotes.Count ); quoteResult = quotes[number]; } return quoteResult; } }
the style sheet such as it is
body { } h1 { background-color:navy; color:white; } .response { color:green; }
No comments:
Post a Comment