How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach &...

29
Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Transcript of How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach &...

Page 1: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1

Chapter 6

How to use the standard server controls

Page 2: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 2

Common attributes of list box controls Rows

SelectionMode

Common attributes of list items Text

Value

Selected

Page 3: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 3

The aspx for a list box <asp:ListBox ID="lstContactVia" runat="server"

SelectionMode="Multiple">

<asp:ListItem Selected="True">Twitter</asp:ListItem>

<asp:ListItem>Facebook</asp:ListItem>

<asp:ListItem Value="Text">Text

Message</asp:ListItem>

<asp:ListItem>Email</asp:ListItem>

</asp:ListBox>

Page 4: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 4

The Collection Editor for creating and editing lists

Page 5: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 5

Common properties of list controls

SelectedItem

SelectedIndex

SelectedValue

A common event of all list controls SelectedIndexChanged

Page 6: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 6

The aspx code for a drop-down list <asp:DropDownList ID="ddlDay" runat="server">

<asp:ListItem Value="1">Sunday</asp:ListItem>

<asp:ListItem Value="2">Monday</asp:ListItem>

<asp:ListItem Value="3">Tuesday</asp:ListItem>

<asp:ListItem Value="4">Wednesday</asp:ListItem>

<asp:ListItem Value="5">Thursday</asp:ListItem>

<asp:ListItem Value="6">Friday</asp:ListItem>

<asp:ListItem Value="7">Saturday</asp:ListItem>

</asp:DropDownList>

Page 7: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 7

Code that gets the value of the selected item in the drop-down list

int dayNumber = Convert.ToInt32(ddlDay.SelectedValue);

Code that gets the text for the selected item string dayName = ddlDay.SelectedItem.Text;

Code that uses the SelectedIndexChanged event protected void ddlDay_SelectedIndexChanged(

object sender, EventArgs e)

{

int dayNumber =

Convert.ToInt32(ddlDay.SelectedValue);

}

Page 8: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 8

Common property of a list control

Items

Common property of a ListItemCollection object Count

Common indexer of a ListItemCollection object [integer]

Page 9: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 9

Common methods of a ListItemCollection object

Add(string)

Add(ListItem)

Insert(integer, string)

Insert(integer, ListItem)

Remove(string)

Remove(ListItem)

RemoveAt(integer)

Clear()

Page 10: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 10

C# code that loads items into a drop-down list using strings

for (int i = 50; i <= 500; i += 50)

{

ddlMonthlyInvestment.Items.Add(i.ToString());

}

C# code that loads items into a drop-down list using ListItem objects

ddlDay.Items.Add(New ListItem("Sunday", "1"));

ddlDay.Items.Add(New ListItem("Monday", "2"));

ddlDay.Items.Add(New ListItem("Tuesday", "3"));

Page 11: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 11

Attributes for formatting radio button and check box lists

RepeatLayout

RepeatDirection

RepeatColumns

Page 12: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 12

A check box list and a radio button list in a browser

Page 13: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 13

The aspx code for the check box list Please let me know about:

<asp:CheckBoxList ID="cblAboutList" runat="server"

RepeatDirection="Horizontal">

<asp:ListItem Value="New" Selected="True">

New products</asp:ListItem>

<asp:ListItem Value="Special">

Special offers</asp:ListItem>

<asp:ListItem Value="Revisions">

New editions</asp:ListItem>

</asp:CheckBoxList>

A statement that checks if the first item in a check box list is selected

if (cblAboutList.Items[0].Selected) ...

Page 14: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 14

The aspx code for the radio button list Please contact me via:

<asp:RadioButtonList ID="rblContactVia" runat="server" >

<asp:ListItem Selected="True">Twitter</asp:ListItem>

<asp:ListItem>Facebook</asp:ListItem>

</asp:RadioButtonList>

A statement that gets the value of the selected item in a radio button list

customer.ContactVia = rblContactVia.SelectedValue;

Page 15: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 15

A CheckOut page with standard server controls

Page 16: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 16

The head section in the HTML for the page <head runat="server">

<title>Chapter 6: Shopping Cart</title>

<link href="Styles/Main.css" rel="stylesheet" />

<link href="Styles/CheckOut.css" rel="stylesheet" />

</head>

Page 17: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 17

The aspx code for the form on the CheckOut page <form id="form1" runat="server" DefaultFocus="txtEmail1"

DefaultButton="btnCheckOut">

<h2>Contact information</h2>

<label>Email: </label>

<asp:TextBox ID="txtEmail1" runat="server"

CssClass="entry" TextMode="Email" >

</asp:TextBox><br />

<label>Email Re-entry: </label>

<asp:TextBox ID="txtEmail2" runat="server"

CssClass="entry" >

</asp:TextBox><br />

<%-- labels and text boxes for first name, last name,

and phone --%>

Page 18: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 18

The aspx code for the CheckOut form (cont.) <h2>Billing address</h2>

<%-- labels and text boxes for address and city --%>

<label>State: </label>

<asp:DropDownList ID="ddlState" runat="server"

CssClass="entry" AppendDataBoundItems="True"

DataSourceID="SqlDataSource1"

DataTextField="StateName"

DataValueField="StateCode">

<asp:ListItem Text="" Value=""

Selected="True"></asp:ListItem>

</asp:DropDownList><br />

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString=

"<%$ ConnectionStrings:HalloweenConnection %>"

SelectCommand="SELECT [StateCode], [StateName]

FROM [States]

ORDER BY [StateCode]">

</asp:SqlDataSource>

Page 19: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 19

The aspx code for the CheckOut form (cont.) <label>Zip code: </label>

<asp:TextBox ID="txtZip" runat="server"

CssClass="entry"

MaxLength="5"></asp:TextBox>

<h2>Optional data</h2>

<div id="optionalData">

Please let me know about:

<asp:CheckBoxList ID="cblAboutList"

runat="server" RepeatColumns="2">

<asp:ListItem Value="New">

New products</asp:ListItem>

<asp:ListItem Value="Special">

Special offers</asp:ListItem>

<asp:ListItem Value="Revisions">

New editions</asp:ListItem>

<asp:ListItem Value="Local">

Local events</asp:ListItem>

</asp:CheckBoxList>

Page 20: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 20

The aspx code for the CheckOut form (cont.) Please contact me via:

<asp:RadioButtonList ID="rblContactVia"

runat="server" RepeatDirection="Horizontal">

<asp:ListItem Selected="True">Twitter

</asp:ListItem>

<asp:ListItem>Facebook</asp:ListItem>

<asp:ListItem Value="Text">Text Message

</asp:ListItem>

<asp:ListItem>Email</asp:ListItem>

</asp:RadioButtonList>

</div>

Page 21: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 21

The aspx code for the CheckOut form (cont.) <asp:Button ID="btnCheckOut" runat="server"

Text="Check Out" CssClass="button"

OnClick="btnCheckOut_Click" />

<asp:Button ID="btnCancel" runat="server"

Text="Cancel Order" CausesValidation="False"

OnClick="btnCancel_Click" CssClass="button" />

<asp:LinkButton ID="lbtnContinueShopping"

runat="server" PostBackUrl="~/Order.aspx"

CausesValidation="False">Continue Shopping

</asp:LinkButton>

</form>

Page 22: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 22

The code-behind file for capturing the data public partial class CheckOut : System.Web.UI.Page

{

private Customer customer;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

customer = (Customer)Session["Customer"];

this.LoadCustomerData();

}

}

protected void btnCheckOut_Click(

object sender, EventArgs e)

{

if (Page.IsValid)

{

this.GetCustomerData();

Response.Redirect("~/CheckOut2.aspx");

}

}

Page 23: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 23

The code-behind file for capturing the data (cont.) protected void btnCancel_Click(

object sender, EventArgs e)

{

Session.Remove("Cart");

Session.Remove("Customer");

Response.Redirect("~/Order.aspx");

}

Page 24: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 24

The code-behind file for capturing the data (cont.) private void LoadCustomerData()

{

if (customer != null)

{

txtFirstName.Text = customer.FirstName;

// load data into other text boxes

// from customer object

ddlState.SelectedValue = customer.State;

rblContactVia.SelectedValue =

customer.ContactVia;

cblAboutList.Items[0].Selected =

customer.NewProductsInfo;

cblAboutList.Items[1].Selected =

customer.SpecialPromosInfo;

cblAboutList.Items[2].Selected =

customer.NewRevisionsInfo;

cblAboutList.Items[3].Selected =

customer.LocalEventsInfo;

}

}

Page 25: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 25

The code-behind file for capturing the data (cont.) private void GetCustomerData()

{

if (customer == null)

customer = new Customer();

customer.FirstName = txtFirstName.Text;

// get data from the other text boxes and

// load into customer object

customer.State = ddlState.SelectedValue;

customer.ContactVia = rblContactVia.SelectedValue;

customer.NewProductsInfo =

cblAboutList.Items[0].Selected;

customer.SpecialPromosInfo =

cblAboutList.Items[1].Selected;

customer.NewRevisionsInfo =

cblAboutList.Items[2].Selected;

customer.LocalEventsInfo =

cblAboutList.Items[3].Selected;

Session["Customer"] = customer;

}

}

Page 26: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 26

Other standard server controls that you may want to use

Calendar

AdRotator

Wizard

MultiView

View

Panel

Page 27: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 27

Three steps of a Wizard control

Page 28: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 28

The starting aspx for a Wizard control <asp:Wizard ID="Wizard1" runat="server"

ActiveStepIndex="0">

<WizardSteps>

<asp:WizardStep runat="server" Title="Step 1">

<h2>Contact information</h2>

...

</asp:WizardStep>

<asp:WizardStep runat="server" Title="Step 2">

<h2>Shipping method</h2>

...

</asp:WizardStep>

<asp:WizardStep runat="server" Title="Step 3">

<h2>Credit card information</h2>

...

</asp:WizardStep>

</WizardSteps>

</asp:Wizard>

Page 29: How to use the standard - ptcdb.edu.ps€¦ · Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 6 How to use the standard server controls

Murach's ASP.NET 4.5/C#, C6 © 2013, Mike Murach & Associates, Inc. Slide 29

Extra 6-1 Build a Reservation form