Event Driven Programming Source Code Files == WinformsEvents == Form1.cs =========================================== // WinFormsEvents Example // Illustrate the Form events. using System; using System.Drawing; using System.Windows.Forms; namespace WinformsEvents { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnShow_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show("Form1, Load Event"); } private void Form1_Activated(object sender, EventArgs e) { textBox1.Text = "Form1, Activated Event"; } private void Form1_Deactivate(object sender, EventArgs e) { textBox1.Text = "Form1, Deactivate Event"; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r = MessageBox.Show( "Do you really want to close?", "Close Query", MessageBoxButtons.YesNo); if (r == DialogResult.No) { e.Cancel = true; } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { MessageBox.Show("Form1, FormClosed Event"); } } } -- WinFormsEvents -- Form2.cs ------------------------------------------- // FormEvents Example // Illustrate the Form events. using System; using System.Drawing; using System.Linq; using System.Windows.Forms; namespace WinformsEvents { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { MessageBox.Show("Form2, Load Event"); } private void Form2_Activated(object sender, EventArgs e) { textBox1.Text = "Form2, Activated Event"; } private void Form2_Deactivate(object sender, EventArgs e) { textBox1.Text = "Form2, Deactivate Event"; } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r = MessageBox.Show( "Do you really want to close Form2?", "Close Query", MessageBoxButtons.YesNo); if (r == DialogResult.No) { e.Cancel = true; } } private void Form2_FormClosed(object sender, FormClosedEventArgs e) { MessageBox.Show("Form2, FormClosed Event"); } } } == MonthlyPayment == Form1.cs =========================================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace MonthlyPayment { /// <summary> /// FutureValue1 Example /// Compute the monthly payment for a loan, given the /// Use the Microsoft.VisualBasic.Financial.FV /// principal, term in years, and interest rate. /// </summary> public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnReset_Click(object sender, EventArgs e) { txtPrincipal.Clear(); txtTermInYears.Clear(); txtInterest.Clear(); txtFutureValue.Clear(); txtPrincipal.Focus(); } private void btnCompute_Click(object sender, EventArgs e) { double principal, interest, monthlyRate, monthlyPayment; int termInYears, termInMonths; bool principalFlag, termInYearsFlag, interestFlag; // Make sure that input textboxes have numeric values. principalFlag = double.TryParse(txtPrincipal.Text, out principal); termInYearsFlag = int.TryParse(txtTermInYears.Text, out termInYears); interestFlag = double.TryParse(txtInterest.Text, out interest); if (!principalFlag || !termInYearsFlag || !interestFlag) return; // Compute monthly payment. termInMonths = termInYears * 12; monthlyRate = interest / (12 * 100); monthlyPayment = (principal * monthlyRate) / (1.0 - Math.Pow(1.0 + monthlyRate, -termInMonths)); txtFutureValue.Text = monthlyPayment.ToString("$#,##0.00"); } private void EnterHandler(object sender, EventArgs e) { // Change text to red when control gains focus. Control c; c = (Control)sender; c.ForeColor = Color.Red; } private void LeaveHandler(object sender, EventArgs e) { // Change text to red when control gains focus. Control c; c = (Control)sender; c.ForeColor = Color.Black; } } } == ToUpper1 == WebForm1.aspx ============================================ <!-- ToUpper1 Example Convert string to uppercase. --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ToUpper1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>ToUpper1 Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>ToUpper1 Example</h2> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtString" runat="server" CssClass="ctrl"> This is a test string. </asp:TextBox> <br /> <br /> <asp:Button ID="btnConvert" runat="server" CssClass="ctrl" Text="Convert to Uppercase" OnClick="btnConvert_Click" /> </div> </form> </body> </html> -- ToUpper1 -- WebForm1.aspx.cs ----------------------------------------- // ToUpper1 Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ToUpper1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnConvert_Click(object sender, EventArgs e) { txtString.Text = txtString.Text.ToUpper(); } } } -- ToUpper1 -- Styles.css ----------------------------------------------- /* ToUpper1 Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .ctrl { width: 250px; font-size: 100%; } == ToUpper2 == WebForm1.aspx ============================================ <!-- ToUpper2 Example Convert string to uppercase. Move the code from the code-behind file to a script in the ASP.Net page. --> <%@ Page Language="C#" %> <script runat="server"> protected void btnConvert_Click(object sender, EventArgs e) { txtString.Text = txtString.Text.ToUpper(); } </script> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>ToUpper1 Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>ToUpper2 Example</h2> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtString" runat="server" CssClass="ctrl"> This is a test string. </asp:TextBox> <br /> <br /> <asp:Button ID="btnConvert" runat="server" CssClass="ctrl" Text="Convert to Uppercase" OnClick="btnConvert_Click" /> </div> </form> </body> </html> -- ToUpper2 -- WebForm1.aspx -------------------------------------------- /* ToUpper2 Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .ctrl { width: 250px; font-size: 100%; } == ShowPoem == WebForm1.aspx ============================================ <!-- ShowPoem Example Display the contents of a textfile on page. --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ShowPoem.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ShowPoemExample</title> <style type="text/css"> </style> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>ShowPoem Example</h2> <p>Display poem obtained from text file raven.txt.</p> <form id="frmShowPoem" runat="server"> <p> <asp:Literal ID="litPoem" runat="server" /> </p> </body> </html> -- ShowPoem -- WebForm1.aspx.cs ----------------------------------------- // ShowPoem Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ShowPoem { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { System.IO.StreamReader sr; sr = new System.IO.StreamReader("c:/jost/raven.txt"); string line; string output = ""; while (sr.Peek() > -1) { line = sr.ReadLine(); output += line + "<br />"; } litPoem.Text = output; } } } } -- ShowPoem -- Styles.css ----------------------------------------------- /* ShowPoem Example */ body { background-color: #909090; color: #600000; } h2,h3 { font-family: Forte; } h2 { font-size: 200%; } h3 { font-size: 160%; } p { font-family: Papyrus; } -- ShowPoem -- raven.txt ------------------------------------------------ The Raven by Edgar Allan Poe First Published in 1845 Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore, While I nodded, nearly napping, suddenly there came a tapping, As of someone gently rapping, rapping at my chamber door. " 'Tis some visitor," I muttered, "tapping at my chamber door; Only this, and nothing more." ... 17 more stanzas. == Quiz1 == WebForm1.aspx =============================================== <!-- Quiz1 Example Use RadioButton groups to input the answers to a quiz. --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Quiz1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Quiz1 Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Quiz1 Example</h2> <div> <form id="form2" runat="server"> <ol> <li>Who was the first president of the United States?<br /> <asp:RadioButton ID="radQ1a" GroupName="Q1" Text="Abraham Lincoln" runat="server" /><br /> <asp:RadioButton ID="radQ1b" GroupName="Q1" Text="George Washington" runat="server" /><br /> <asp:RadioButton ID="radQ1c" GroupName="Q1" Text="John Adams" runat="server" /><br /> <asp:RadioButton ID="radQ1d" GroupName="Q1" Text="Thomas Jefferson" runat="server" /><br /> </li><br /> <li>Who was the oldest president at inauguration<br /> <asp:RadioButton ID="radQ2a" GroupName="Q2" Text="Franklin Delanor Roosevelt" runat="server" /><br /> <asp:RadioButton ID="radQ2b" GroupName="Q2" Text="Ronald Reagan" runat="server" /><br /> <asp:RadioButton ID="radQ2c" GroupName="Q2" Text="Theodore Roosevelt" runat="server" /><br /> <asp:RadioButton ID="radQ2d" GroupName="Q2" Text="Warren Harding" runat="server" /><br /> </li><br /> <li>Who was the youngest president at inauguration<br /> <asp:RadioButton ID="radQ3a" GroupName="Q3" Text="Bill Clinton" runat="server" /><br /> <asp:RadioButton ID="radQ3b" GroupName="Q3" Text="George W. Bush" runat="server" /><br /> <asp:RadioButton ID="radQ3c" GroupName="Q3" Text="John Kennedy" runat="server" /><br /> <asp:RadioButton ID="radQ3d" GroupName="Q3" Text="Theodore Roosevelt" runat="server" /><br /> </li><br /> <li>Who was the first president to be inaugurated on January 20?<br /> <asp:RadioButton ID="radQ4a" GroupName="Q4" Text="Franklin Delanor Roosevelt" runat="server" /><br /> <asp:RadioButton ID="radQ4b" GroupName="Q4" Text="Harry Truman" runat="server" /><br /> <asp:RadioButton ID="radQ4c" GroupName="Q4" Text="Herbert Hoover" runat="server" /><br /> <asp:RadioButton ID="radQ4d" GroupName="Q4" Text="Woodrow Wilson" runat="server" /><br /> </li> </ol> <asp:Button ID="btnSubmit" runat="server" Text="Submit Answers" CssClass="button" onclick="btnSubmit_Click" Width="233px" /> <br /> <br /> <asp:Literal ID="litOutput" runat="server" Text="" /> </form> </div> </form> </body> </html> -- Quiz1 -- WebForm1.aspx.cs -------------------------------------------- using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; // Quiz1 Example namespace Quiz1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { int totalCorrect = 0; if (radQ1b.Checked) { totalCorrect++; } if (radQ2b.Checked) { totalCorrect++; } if (radQ3d.Checked) { totalCorrect++; } if (radQ4a.Checked) { totalCorrect++; } litOutput.Text = "Total correct answers is " + totalCorrect + "."; } } } -- Quiz1 -- Styles.css -------------------------------------------------- /* Quiz1 Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .textbox { width: 200px; font-size: 100%; text-align: right; } .button { width: 200px; font-size: 100%; } == Animals == WebForm1.aspx ============================================= <!-- Animals Example Add, view, or delete animals using C# code. --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Animals.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Animals Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Animals Example</h2> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtSelectedAnimal" runat="server" CssClass="ctrl" /><br /> <asp:Label ID="Label1" runat="server" CssClass="ctrl" Text="Selected Animal" /> <br /> <br /> <asp:Button ID="btnShowSelected" runat="server" CssClass="ctrl" Text="Show Selected" OnClick="btnShowSelected_Click"/> <br /> <br /> <asp:Button ID="btnDeleteSelected" runat="server" CssClass="ctrl" Text="Delete Selected" OnClick="btnDeleteSelected_Click"/> <br /> <br /> <asp:ListBox ID="lsbAnimals" runat="server" CssClass="ctrl" Height="175px"> <asp:ListItem Value="Oppossum">Oppossum</asp:ListItem> <asp:ListItem Value="Raccoon">Raccoon</asp:ListItem> <asp:ListItem Value="Deer">Deer</asp:ListItem> </asp:ListBox> </div> </form> </body> </html> -- Animals == WebForm1.aspx.cs ------------------------------------------ // Animals Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Animals { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { lsbAnimals.Items.Add(new ListItem("Fox", "Fox")); lsbAnimals.Items.Add(new ListItem("Wolf", "Wolf")); lsbAnimals.Items.Add(new ListItem("Bear", "Bear")); } } protected void btnShowSelected_Click(object sender, EventArgs e) { if (lsbAnimals.SelectedIndex >= 0) { txtSelectedAnimal.Text = lsbAnimals. SelectedItem.ToString(); } else { txtSelectedAnimal.Text = ""; } } protected void btnDeleteSelected_Click(object sender, EventArgs e) { int index = lsbAnimals.SelectedIndex; if (index > 0) { lsbAnimals.Items.RemoveAt(index); } } } } -- Animals -- Styles.css ------------------------------------------------ /* Animals Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .ctrl { width: 170px; font-size: 100%; } == Quiz2 == WebForm1.aspx =============================================== <!-- Quiz2 Example Same as the Quiz1 Example, but use RadioButtonList controls instead. --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Quiz2 Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Quiz2 Example</h2> <form id="form2" runat="server"> <div> <ol> <li>Who was the first president of the United States?<br /> <asp:RadioButtonList ID="rblQ1" runat="server"> <asp:ListItem>Abraham Lincoln</asp:ListItem> <asp:ListItem>George Washington</asp:ListItem> <asp:ListItem>John Adams</asp:ListItem> <asp:ListItem>Thomas Jefferson</asp:ListItem> </asp:RadioButtonList> <br /> </li> <li>Who was the oldest president at inauguration?<br /> <asp:RadioButtonList ID="rblQ2" runat="server"> <asp:ListItem>Franklin Delanor Roosevelt</asp:ListItem> <asp:ListItem>Ronald Reagan</asp:ListItem> <asp:ListItem>Warren Harding</asp:ListItem> <asp:ListItem>William Taft</asp:ListItem> </asp:RadioButtonList> <br /> </li> <li>Who was the youngest president at inauguration?<br /> <asp:RadioButtonList ID="rblQ3" runat="server"> <asp:ListItem>Bill Clinton</asp:ListItem> <asp:ListItem>George W. Bush</asp:ListItem> <asp:ListItem>John Kennedy</asp:ListItem> <asp:ListItem>Theodore Roosevelt</asp:ListItem> </asp:RadioButtonList> <br /> </li> <li>Who was the first president to be inaugurated on January 20?<br /> <asp:RadioButtonList ID="rblQ4" runat="server"> <asp:ListItem>Franklin Delanor Roosevelt</asp:ListItem> <asp:ListItem>Harry Truman</asp:ListItem> <asp:ListItem>Herbert Hoover</asp:ListItem> <asp:ListItem>Woodrow Wilson</asp:ListItem> </asp:RadioButtonList> <br /> </li> </ol> <asp:Button ID="btnSubmit" runat="server" Text="Submit Answers" CssClass="button" onclick="btnSubmit_Click" Width="233px" /> <br /> <br /> <asp:Literal ID="litOutput" runat="server" Text="" /> </div> </form> </body> </html> -- Quiz2 -- WebForm1.aspx ----------------------------------------------- // Quiz2 Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { int totalCorrect = 0; if (rblQ1.SelectedIndex == 1) { totalCorrect++; } if (rblQ2.SelectedIndex == 1) { totalCorrect++; } if (rblQ3.SelectedIndex == 3) { totalCorrect++; } if (rblQ4.SelectedIndex == 0) { totalCorrect++; } litOutput.Text = "Total correct answers is " + totalCorrect + "."; } } } -- Quiz2 -- Styles.css -------------------------------------------------- /* Quiz2 Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .textbox { width: 200px; font-size: 100%; text-align: right; } .button { width: 200px; font-size: 100%; } == ChineseZodiac == WebForm1.aspx ======================================= <!-- ChineseZodiac Example Show the Chinese Zodiac animal that corresponds to year of birth --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ChineseZodiac.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>ChineseZodiac Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>ChineseZodiac Example</h2> <form id="form2" runat="server"> <div> Year of Birth <asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged" CssClass="textbox"> <asp:ListItem Text="1980" Value="1980" /> <asp:ListItem Text="1981" Value="1981" /> ... Years between 1981 and 2011 are omitted. <asp:ListItem Text="2011" Value="2011" /> <asp:ListItem Text="2012" Value="2012" /> </asp:DropDownList> <br /> <br /> <asp:Literal ID="litOutput" runat="server" /> </div> </form> </body> </html> -- ChineseZodiac -- WebForm1.aspx.cs ------------------------------------ // ChineseZodiac Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ChineseZodiac { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e) { string animal; int year = int.Parse(ddlYear.Text); switch (year % 12) { case 0: animal = "monkey"; break; case 1: animal = "rooster"; break; case 2: animal = "dog"; break; case 3: animal = "pig"; break; case 4: animal = "rat"; break; case 5: animal = "ox"; break; case 6: animal = "tiger"; break; case 7: animal = "rabbit"; break; case 8: animal = "dragon"; break; case 9: animal = "snake"; break; case 10: animal = "horse"; break; default: animal = "sheep"; break; } litOutput.Text = "The Chinese zodiac animal for the year " + year + " is the " + animal + "."; } } } -- ChineseZodiac -- Styles.css ------------------------------------------ /* Chinese Zodiac*/ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .textbox { width: 200px; font-size: 100%; text-align: right; } .button { width: 200px; font-size: 100%; } == Beverage == WebForm1.aspx ============================================ <!-- Beverage Example Order your breakfast drink online. Only cold orange juice is allowed. --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Beverage.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Beverage Example</h2> <form id="form1" runat="server"> <div> <p>Beverage<br /> <asp:RadioButtonList ID="radListBeverage" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList_SelectedIndexChanged"> <asp:ListItem Value="coffee" Selected="True">Coffee</asp:ListItem> <asp:ListItem Value="oj">Orange Juice</asp:ListItem> <asp:ListItem Value="tea">Tea</asp:ListItem> </asp:RadioButtonList> </p> <p>Temperature<br /> <asp:RadioButtonList ID="radListTemperature" runat="server" AutoPostBack="false"> <asp:ListItem Value="cold">Cold</asp:ListItem> <asp:ListItem Value="hot" Selected="True">Hot</asp:ListItem> </asp:RadioButtonList> </p> <p>Extras<br /> <asp:CheckBoxList ID="chkListExtras" runat="server" AutoPostBack="False"> <asp:ListItem Selected="False">Cream</asp:ListItem> <asp:ListItem Selected="False">Sugar</asp:ListItem> </asp:CheckBoxList> </p> <p><asp:Button ID="btnSubmit" runat="server" Text="Submit Beverage Order" OnClick="btnSubmitOrder_Click" CssClass="ctrl" Width="291px" /> </p> <p><asp:Literal ID="litOrder" runat="server" /> </p> </div> </form> </body> </html> -- Beverage -- WebForm1.aspx.cs ----------------------------------------- // Beverage Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Beverage { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmitOrder_Click(object sender, EventArgs e) { string beverage, temperature, extras; beverage = radListBeverage.SelectedValue; temperature = radListTemperature.SelectedValue; if (chkListExtras.Items[0].Selected && chkListExtras.Items[1].Selected) { extras = "; cream, sugar"; } else if (chkListExtras.Items[0].Selected && !chkListExtras.Items[1].Selected) { extras = "; cream"; } else if (!chkListExtras.Items[0].Selected && chkListExtras.Items[1].Selected) { extras = "; sugar"; } else { extras = ""; } litOrder.Text = "Order: " + beverage + "; " + temperature + "" + extras; } protected void RadioButtonList_SelectedIndexChanged( object sender, EventArgs e) { // Only cold orange juice is allowed. if (radListBeverage.SelectedValue == "oj") { radListTemperature.SelectedIndex = 0; chkListExtras.Items[0].Selected = false; chkListExtras.Items[1].Selected = false; radListTemperature.Enabled = false; chkListExtras.Enabled = false; } else { radListTemperature.Enabled = true; chkListExtras.Enabled = true; radListTemperature.Items[1].Selected = true; } } } } -- Beverage -- Styles.css ----------------------------------------------- /* Beverage Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .ctrl { width: 250px; font-size: 100%; } == Greeter3 == WebForm1.aspx ============================================ <!-- Greeter3 Example Like Greeter2 but as web applicaion --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Greeter3.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Greeter3 Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Greeter3 Example</h2> <form id="form1" runat="server"> <div id="buttons"> <asp:RadioButtonList ID="rblLanguage" runat="server" AutoPostBack="True" OnSelectedIndexChanged= "rblLanguage_SelectedIndexChanged"> <asp:ListItem Value="English"></asp:ListItem> <asp:ListItem Value="French"></asp:ListItem> <asp:ListItem Value="German"></asp:ListItem> <asp:ListItem Value="Spanish"></asp:ListItem> </asp:RadioButtonList> <br /> <asp:TextBox ID="txtName" runat="server" CssClass="textbox" /> <br /> <br /> <asp:Button ID="btnShowGreeting" runat="server" Text="Show Greeting" CssClass="button" OnClick="btnShowGreeting_Click" /> </div> <div id="img"> <asp:TextBox ID="txtGreeting" Width="400px" runat="server" CssClass="textbox" /> <br /> <br /> <asp:Image ID="imgCountry" runat="server" /> </div> </form> </body> </html> -- Greeter3 -- WebForm1.aspx.cs ----------------------------------------- //Greeter3 Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Greeter3 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { imgCountry.ImageUrl = "~/English.jpg"; rblLanguage.SelectedIndex = 0; } } protected void btnShowGreeting_Click(object sender, EventArgs e) { string name = txtName.Text; if (rblLanguage.Items[0].Selected) { txtGreeting.Text = "Good day. " + "\r\nHow are you, " + name + "?"; imgCountry.ImageUrl = "~/English.jpg"; } else if (rblLanguage.Items[1].Selected) { txtGreeting.Text = "Bon jour. " + "\r\nComment ça va, " + name + "?"; imgCountry.ImageUrl = "~/French.jpg"; } else if (rblLanguage.Items[2].Selected) { txtGreeting.Text = "Guten Tag. " + "\r\nWie gehen Sie, " + name + "?"; imgCountry.ImageUrl = "~/German.jpg"; } else if (rblLanguage.Items[3].Selected) { txtGreeting.Text = "Buenos Días. " + "\r\n¿Como estás, " + name + "?"; imgCountry.ImageUrl = "~/Spanish.jpg"; } } protected void rblLanguage_SelectedIndexChanged( object sender, EventArgs e) { imgCountry.ImageUrl = "~/" + ((RadioButtonList)sender).SelectedItem.Text + ".jpg"; } } } -- Greeter3 -- Styles.css ----------------------------------------------- /* Greeter3 Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } #buttons { position: absolute; top: 100px; left: 20px; } #img { position: absolute; top: 100px; left: 300px; } .textbox { width: 200px; font-size: 100%; text-align: left; } .button { width: 200px; font-size: 100%; } == HtmlControls == WebForm1.aspx ======================================== <!-- HtmlControls Example Show traditional HTML buttons with client-side JavaScript scripts --> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="HtmlControls.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>HtmlControls Example</title> <link href="Styles.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> ClickHandler = function () { document.getElementById("txtMsg").value = "I\'ve been clicked."; } </script> </script> </head> <body> <h2>HtmlControls Example</h2> <form id="form1" runat="server"> <div> <input id="txtMsg" type="text" class="ctrl" /> <br /> <br /> <input id="Button1" type="button" value="Show Message 1" class="ctrl" onclick="alert('I\'ve been clicked');" /> <br /> <br /> <input id="Button2" type="button" value="Show Message 2" class="ctrl" onclick="document.getElementById('txtMsg').value = 'I\'ve been clicked.';" /> <br /> <br /> <input id="Button3" type="button" value="Show Message 3" class="ctrl" onclick="ClickHandler();" /> </div> </form> </body> </html> -- HtmlControls == WebForm1.aspx.cs ------------------------------------- // HtmlControls Example using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace HtmlControls { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } } -- HtmlControls -- Styles.css ------------------------------------------- /* HtmlControls Example */ body { font-family: Verdana; font-size: 130%; background-color: beige; color: navy; } h2 { color: maroon; } .ctrl { width: 250px; font-size: 100%; } =========================================================================