Tuesday, November 29, 2011

Morning ASP Examples

Here is the ASP source code


<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="FinalProject.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
      <p> Enter your Name <asp:TextBox 
ID="txtName" runat="server"></asp:TextBox></p>
        <asp:Button ID="btnName" runat="server" Text="Click Me" 
            onclick="btnName_Click" />
        <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Value="3.25">Bread</asp:ListItem>
            <asp:ListItem Value="1.95">Eggs</asp:ListItem>
            <asp:ListItem Value="3">Milk</asp:ListItem>
            <asp:ListItem Value="1.59">Chili</asp:ListItem>
            <asp:ListItem Value="7.99">Beer</asp:ListItem>
        </asp:CheckBoxList>
        
        <asp:Button ID="Button1" runat="server" 
onclick="Button1_Click" Text="GetTotal" />
        
        <asp:Label ID="lblTotal" runat="server" 
Text="Label"></asp:Label>
        
    </div>
    </form>
</body>
</html>


Here is the code behind

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
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    CheckBoxList1.Items.Add("Red");
        //    CheckBoxList1.Items.Add("Green");
        //    CheckBoxList1.Items.Add("Blue");
        //}
    }
    protected void btnName_Click(object sender, EventArgs e)
    {
        lblResult.Text = "Hello " + txtName.Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        double total = 0;
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                total += double.Parse(item.Value.ToString());
               
            }

            lblTotal.Text = "Your total is " + total.ToString("c"); 
        }
    }
}

No comments:

Post a Comment