Here is the web page Default.aspx
<%@ 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="BoreingStuff.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Quote Generator</h1>
<p>
Enter a favorite Quote:
<asp:TextBox ID="txtQuote" runat="server"></asp:TextBox> <br />
<asp:Button ID="btnEnter" runat="server" Text="Enter" OnClick="btnEnter_Click" />
</p>
<p>
<asp:Button ID="btnGetQuote" runat="server" Text="Get Quote" OnClick="btnGetQuote_Click" />
<asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
</p>
</div>
</form>
</body>
</html>
Here is the code behind Default.aspx.cs
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
{
//create a new instance of the Quotes class
Quotes q = new Quotes();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnEnter_Click(object sender, EventArgs e)
{
//see if the session has something in it
if (Session["quotes"] != null)
{
//if it does use it instead of a new Quotes object
q = (Quotes)Session["quotes"];
}
//write the quote from the textbox into the list
q.AddQuote(txtQuote.Text);
//save the object to the session
Session["quotes"] = q;
//clear the textbox
txtQuote.Text = "";
}
protected void btnGetQuote_Click(object sender, EventArgs e)
{
//recall the session and get the quote object
q = (Quotes)Session["quotes"];
//call the GetQuote method and assign the string
//to the label
lblResult.Text = q.GetQuote();
}
}
Here is the Quotes.cs class
using System; using System.Collections.Generic; using System.Linq; using System.Web; ////// Summary description for Quotes /// this class takes quotes /// and stores them in a list object /// the list object behaves a lot like /// an array but you don't have to /// give it a fixed size /// public class Quotes { //declaring a list object//is generic syntax. It assigns a //type for the list at runtime private List myQuotes; public Quotes() { //initialize the list in the constructor myQuotes = new List (); } public List MyQuotes { //will let the user get the list get { return myQuotes; } } public void AddQuote(string quote) { //adds a quote to the list MyQuotes.Add(quote); } public string GetQuote() { //create a random object Random rand = new Random(); // get a number between 0 and the number of elements //in the list (minus 1 to account for starting at 0) int index = rand.Next(0, MyQuotes.Count - 1); //return the quote at that index return myQuotes[index]; } }
And for what it's worth here is the boreingstuff.css file
body {
}
h1 {
color:white;
background-color:navy;
}
.testclass {
color: green;
}
No comments:
Post a Comment