murach’s asp.net 4/c#, c6© 2011, mike murach & associates, inc.slide 1

61
Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 1 C hapter6 How to w ork w ith servercontrols

Upload: jamison-cumberland

Post on 14-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 1

Chapter 6

How to work with server controls

Page 2: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 2

Objectives

Applied

Given the specifications for a web form that uses any of the server controls presented in this chapter, design and code the form.

Knowledge

Describe the normal coding technique for handling control events.

Describe the way ASP.NET provides for setting access keys for controls and for setting the default focus and default button for forms.

Describe the differences between buttons, link buttons, and image buttons.

Describe the use of the e argument when working with an image button control.

Page 3: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 3

Objectives (continued) Describe the use of the Command event and the CommandName

property of a button control for processing a group of button controls.

Describe the differences between the way check box controls and radio button controls work, including the difference in when the CheckedChanged event occurs.

Explain how the items in any of the list controls are stored and how you refer to these items in code.

Explain the purpose of the ListItem Collection Editor and describe when you might use it.

Page 4: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 4

Standard controls Commonly used user input controls such as labels, text boxes,

and drop-down lists

Many can be bound to a data source

Data controls Databound user-interface controls that display data via a data

source control

Data source controls that access data from a variety of databases, XML data sources, and business objects

Validation controls Used to validate user input

Work by running client-side script

Can handle most validation requirements

Page 5: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 5

Navigation Controls that provide menus and path maps for navigating a web

site

Login Controls that provide user authentication

AJAX Extensions Controls that provide for updating selected parts of a page during

a postback

Page 6: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 6

The web server controls covered in this chapter Control Name Suggested prefix

Label lbl

TextBox txt

Button btn

LinkButton lbtn

ImageButton ibtn

HyperLink hlnk

DropDownList ddl

ListBox lst

Page 7: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 7

Web server controls (continued) Control Name Suggested prefix

CheckBox chk

CheckBoxList cbl

RadioButton rdo

RadioButtonList rbl

Image img

ImageMap imap

BulletedList blst

Calendar cln

FileUpload upl

Page 8: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 8

Handling a Click event using the OnClick attribute The asp tag for a button control <asp:Button id="btnCancel" runat="server" Text="Cancel Order" OnClick="btnCancel_Click">

The event handler for the Click event of the control protected void btnCancel_Click(object sender, EventArgs e) { Session.Remove("Cart"); Response.Redirect("Order.aspx"); }

Page 9: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 9

Common control events

Event Attribute

Click OnClick

Command OnCommand

TextChanged OnTextChanged

SelectedIndexChanged OnSelectedIndexChanged

CheckedChanged OnCheckedChanged

Page 10: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 10

A form that uses access keys and default focus and button attributes

Page 11: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 11

The aspx code for the form <form id="form1" runat="server" defaultfocus="txtName" defaultbutton="btnNext"> <div id="page"> <p>Please enter your contact information:</p> <p class="text"> <span class="accesskey">N</span>ame:</p> <p class="entry"><asp:TextBox ID="txtName" runat="server" AccessKey="N"></asp:TextBox></p> <p class="text"><span class="accesskey"> E</span>mail:</p> <p class="entry"><asp:TextBox ID="txtEmail" runat="server" AccessKey="E"></asp:TextBox></p> <p id="buttons"> <asp:Button ID="btnPrevious" runat="server" AccessKey="P" Text="Previous" />&nbsp; <asp:Button ID="btnNext" runat="server" AccessKey="N" Text="Next" /> </p> </div> </form>

Page 12: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 12

The CSS for the accesskey class .accesskey { text-decoration: underline; }

A statement that moves the focus to a control txtEmail.Focus

Page 13: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 13

A button, a link button, and an image button in a browser

The asp tags for the three buttons <asp:Button ID="btnAdd" runat="server" Text="Add to Cart" OnClick="btnAdd_Click" /> &nbsp;&nbsp;&nbsp;&nbsp; <asp:LinkButton ID="lbtnCheckOut" runat="server" PostBackUrl="~/CheckOut1.aspx"> Check Out</asp:LinkButton> &nbsp;&nbsp;&nbsp;&nbsp; <asp:ImageButton ID="ibtnCart" runat="server" AlternateText="Cart" ImageUrl="~/Images/cart.gif" PostBackUrl="~/Cart.aspx" />

Page 14: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 14

Common button attributes Text

ImageUrl

AlternateText

CausesValidation

CommandName

CommandArgument

PostBackUrl

Page 15: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 15

An event handler for the Click event of a button control

protected void btnAccept_Click(object sender, EventArgs e) { this.AddInvoice(); Response.Redirect("Confirmation.aspx"); }

Page 16: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 16

An image used for an image button control

The asp tag for the control <asp:ImageButton ID="ibtnNavigate" runat="server" ImageUrl="~/Images/navbuttons.gif" OnClick="ibtnNavigate_Click" />

Page 17: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 17

An event handler for the Click event of the control protected void ibtnNavigate_Click(object sender, ImageClickEventArgs e) { if (e.X >= 0 && e.X <= 23) this.GoToFirstRow(); else if (e.X >= 24 && e.X <= 47) this.GoToPreviousRow(); else if (e.X >= 48 && e.X <= 71) this.GoToNextRow(); else if (e.X >= 72 && e.X <= 95) this.GoToLastRow(); }

Page 18: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 18

Properties of the ImageClickEventArgs class

Property Description

X An integer that represents the x or y coordinate Y where the user clicked the image button.

Page 19: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 19

Four button controls that use the CommandName attribute

<asp:Button ID="btnFirst" runat="server" Text="<<" Width="25px" CommandName="First" OnCommand="NavigationButtons_Command" /> <asp:Button ID="btnPrevious" runat="server" Text="<" Width="25px" CommandName="Previous" OnCommand="NavigationButtons_Command" /> <asp:Button ID="btnNext" runat="server" Text=">" Width="25px" CommandName="Next" OnCommand="NavigationButtons_Command" /> <asp:Button ID="btnLast" runat="server" Text=">>" Width="25px" CommandName="Last" OnCommand="NavigationButtons_Command" />

Page 20: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 20

An event handler for the Command events of the buttons

protected void NavigationButtons_Command(object sender, CommandEventArgs e) { switch (e.CommandName) { case "First": this.GoToFirstRow(); break; case "Previous": this.GoToPreviousRow(); break; case "Next": this.GoToNextRow(); break; case "Last": this.GoToLastRow(); break; } }

Page 21: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 21

Properties of the CommandEventArgs class

Property Description

CommandName The value specified in the CommandName property for the control that generated the Command event.

CommandArgument The value specified in the CommandArgument property for the control that generated the Command event.

Page 22: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 22

A text box and a label displayed in a browser

The asp tag for the text box <asp:TextBox ID="txtQuestion" runat="server" Rows="5" TextMode="MultiLine" Width="296px"></asp:TextBox>

Page 23: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 23

Common text box attributes TextMode

Text

MaxLength

Wrap

ReadOnly

Columns

Rows

Page 24: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 24

The asp tag for the label <asp:Label ID="lblConfirm" runat="server"></asp:Label>

Common label attribute

Attribute Description

Text The text displayed by the label.

Page 25: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 25

Four check boxes and two radio buttons displayed in a browser

Page 26: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 26

The aspx code for the check boxes and radio buttons <asp:CheckBox ID="chkMail" runat="server" Checked="True" Text="Add me to your mailing list" /><br /><br /> Contact me about:<br /> <asp:CheckBox ID="chkSpecial" runat="server" Text="Special offers" /><br /> <asp:CheckBox ID="chkNew" runat="server" Text="New products" /><br /> <asp:CheckBox ID="chkRelated" runat="server" Text="Related products" /><br /><br /> Contact me by:<br /> <asp:RadioButton ID="rdoEmail" runat="server" Checked="True" GroupName="Contact" Text="Email" />&nbsp; <asp:RadioButton ID="rdoPostal" runat="server" GroupName="Contact" Text="Postal mail" />

Page 27: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 27

Common check box and radio button attributes

Attribute Description

Text The text that’s displayed next to the check box or radio button.

Checked Indicates whether the check box or radio button is selected. The default is False.

GroupName The name of the group that the control belongs to (radio buttons only).

Page 28: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 28

Code that retrieves text entered by a user string question = txtQuestion.Text;

Code that changes the Text property of a label lblConfirm.Text = "Thank you for your question.<br />" + "We will respond within 2 business days.";

Page 29: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 29

Code that processes a check box and radio button protected void btnContinue_Click(object sender, EventArgs e) { if (chkMail.Checked) customer.Mail = true; else customer.Mail = false; if (rdoEmail.Checked) customer.MailType = "Email"; else customer.MailType = "Postal"; }

Page 30: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 30

Another way to process the check box and radio buttons protected void chkMail_CheckedChanged(object sender, EventArgs e) { if (chkMail.Checked) customer.Mail = true; else customer.Mail = false; } protected void rdoEmail_CheckedChanged(object sender, EventArgs e) { customer.MailType = "Email"; } protected void rdoPostal_CheckedChanged(object sender, EventArgs e) { customer.MailType = "Postal"; }

Page 31: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 31

A list box displayed in a browser

The asp tag for the list box <asp:ListBox ID="lstColor" runat="server"> <asp:ListItem Value="Black" Selected="True">Black</asp:ListItem> <asp:ListItem Value="Red">Red</asp:ListItem> <asp:ListItem Value="Blue">Blue</asp:ListItem> <asp:ListItem Value="Green">Green</asp:ListItem> </asp:ListBox>

Page 32: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 32

A drop-down list box displayed in a browser

The asp tag for the 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 33: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 33

Common properties of list box and drop-down list controls Items

Rows

SelectedItem

SelectedIndex

SelectedValue

SelectionMode

Page 34: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 34

Common properties of list item objects

Property Description

Text The text that’s displayed for the list item.

Value A string value associated with the list item.

Selected Indicates whether the item is selected.

Page 35: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 35

Retrieving the value of an item in a drop-down list int dayNumber = Convert.ToInt32(ddlDay.SelectedValue);

Retrieving the text for an item in a drop-down list string dayName = ddlDay.SelectedItem.Text;

Using the SelectedIndexChanged event protected void ddlDay_SelectedIndexChanged(object sender, EventArgs e) { int dayNumber = Convert.ToInt32(ddlDay.SelectedValue); }

Page 36: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 36

Common property of list item collection objects Count

Common indexer of list item collection objects [index]

Common methods of list item collection objects Add(string)

Add(ListItem)

Insert(index, string)

Insert(index, ListItem)

Remove(string)

Remove(ListItem)

RemoveAt(index)

Clear()

FindByValue(string)

FindByText(string)

Page 37: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 37

Code that loads items into a list box using strings lstColor.Items.Add("Black"); lstColor.Items.Add("Red"); lstColor.Items.Add("Blue"); lstColor.Items.Add("Green");

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")); ddlDay.Items.Add(new ListItem("Wednesday", "4")); ddlDay.Items.Add(new ListItem("Thursday", "5")); ddlDay.Items.Add(new ListItem("Friday", "6")); ddlDay.Items.Add(new ListItem("Saturday", "7"));

Page 38: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 38

The ListItem Collection Editor dialog box

Page 39: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 39

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

Page 40: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 40

The asp tag for the check box list <asp:CheckBoxList id="cblContact" runat="server" Width="305px" RepeatColumns="2"> <asp:ListItem Value="Special"> Special offers</asp:ListItem> <asp:ListItem Value="New"> New products</asp:ListItem> <asp:ListItem Value="Related"> Related products</asp:ListItem> <asp:ListItem Value="Events"> Local events</asp:ListItem> </asp:CheckBoxList>

Page 41: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 41

The asp tag for the radio button list <asp:RadioButtonList id="rblMail" runat="server" Width="346px" RepeatDirection="Horizontal"> <asp:ListItem Value="Email">Email</asp:ListItem> <asp:ListItem Value="Postal"> Postal mail</asp:ListItem> <asp:ListItem Value="Both" Selected="True"> Both</asp:ListItem> </asp:RadioButtonList>

Page 42: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 42

Attributes for formatting radio button and check box lists

Attribute Description

RepeatLayout Specifies whether ASP.NET should use table tags (Table) an unordered list (UnorderedList), and ordered list (OrderedList), or normal HTML flow (Flow) to format the list when it renders the control. The default is Table.

RepeatDirection Specifies the direction in which the controls should be repeated. The available values are Horizontal and Vertical. The default is Vertical.

RepeatColumns Specifies the number of columns to use when repeating the controls. The default is 0.

Page 43: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 43

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

customer.MailType = rblMail.SelectedValue;

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

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

Page 44: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 44

A bulleted list and a numbered list displayed in a browser

Page 45: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 45

Common attributes of the bulleted list control

Attribute Description

BulletStyle Specifies the bullet style. For a bulleted list, allowable values are Disc, Circle, Square, or CustomImage. For a numbered list, allowable values are Numbered, LowerAlpha, UpperAlpha, LowerRoman, or UpperRoman.

BulletImageUrl Specifies the URL of the image used to display the bullets if the BulletStyle attribute is set to CustomImage.

FirstBulletNumber Specifies the starting number if numbers are displayed.

DisplayMode Specifies how the text for each item should be displayed. Allowable values are Text, HyperLink, or LinkButton. Text is the default.

Page 46: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 46

The aspx code for the bulleted list <asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="Disc"> <asp:ListItem>Styrofoam panel</asp:ListItem> <asp:ListItem> Gray and black latex paint</asp:ListItem> <asp:ListItem>Stone texture paint</asp:ListItem> <asp:ListItem>Rotary tool</asp:ListItem> </asp:BulletedList>

Page 47: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 47

The aspx code for the numbered list <asp:BulletedList ID="BulletedList2" runat="server" BulletStyle="Numbered" DisplayMode="HyperLink"> <asp:ListItem Value="Costumes.aspx">Costumes </asp:ListItem> <asp:ListItem Value="StaticProps.aspx"> Static props</asp:ListItem> <asp:ListItem Value="AnimatedProps.aspx"> Animated props</asp:ListItem> </asp:BulletedList>

A statement that checks if the first link button in a bulleted list was clicked if (blstProjectTypes.Items[0].Selected) ...

Page 48: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 48

Some of the Help documentation for the calendar control

Page 49: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 49

Image control and a hyperlink control in browser

The asp tag for the image control <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Murach logo.jpg" AlternateText="Murach Books" />

Code that sets the URL of an image control imgProduct.ImageUrl = "Images/Products/cat01.jpg";

Page 50: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 50

Common image attributes

Attribute Description

ImageUrl The absolute or relative URL of the image.

AlternateText The text that’s used in place of the image if the browser can’t display the image.

ImageAlign The alignment of the image relative to the web page or other elements on the page. For more information, see online help.

Width The width of the image.

Height The height of the image.

Page 51: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 51

The asp tag for the hyperlink control <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.murach.com">Click here to order </asp:HyperLink>

Common hyperlink attributes

Attribute Description

NavigateUrl The absolute or relative URL of the page that’s displayed when the control is clicked.

Text The text that’s displayed for the control.

ImageUrl The absolute or relative URL of the image that’s displayed for the control.

Page 52: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 52

A file upload control displayed in a browser

The aspx code used to implement the file upload File upload:<br /><br /> <asp:FileUpload ID="uplCustList" runat="server" /> <br /><br /> <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /> <br /><br /> <asp:Label ID="lblMessage" runat="server"></asp:Label>

Page 53: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 53

The Click event procedure for the Upload button protected void btnUpload_Click(object sender, EventArgs e) { int sizeLimit = 5242880; // 5,242,880 is 5MB if (FileUpload1.HasFile) { if (FileUpload1.PostedFile.ContentLength <= sizeLimit) { string path = "C:\\uploads\\" + FileUpload1.FileName; FileUpload1.SaveAs(path); lblMessage.Text = "File uploaded to " + path; } else lblMessage.Text = "File exceeds size limit."; } }

Page 54: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 54

Properties and methods of the FileUpload class

Property Description

HasFile If True, the user has selected a file to upload.

FileName The name of the file to be uploaded.

PostedFile The HttpPostedFile object that represents the file that was posted. You can use this object’s ContentLength property to determine the size of the posted file.

Method Description

SaveAs(string) Saves the posted file to the specified path.

Page 55: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 55

The new_books_2009.jpg image

The aspx code for the image map control <asp:ImageMap ID="imapBooks2009" runat="server" ImageUrl="~/Images/new_books_2009.jpg" HotSpotMode="PostBack" OnClick="imapBooks2009_Click"> <asp:PolygonHotSpot Coordinates="1, 19, 114, 0, 122, 53, 106, 143, 26, 158, 24, 149" PostBackValue="C++" /> <asp:PolygonHotSpot Coordinates="128, 21, 241, 42, 218, 178, 103, 159" PostBackValue="JavaScript" /> </asp:ImageMap>

Page 56: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 56

The Click event procedure for the image map protected void imapBooks2009_Click(object sender, ImageMapEventArgs e) { string book; if (e.PostBackValue.Equals("C++")) book = "C++ 2008"; else book = "JavaScript and DOM Scripting"; }

Page 57: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 57

Common image map attributes

Attribute Description

ImageUrl The URL of the image to be displayed.

HotSpotMode Sets the behavior for the hot spots. PostBack causes the page to be posted and Navigate links to a different page. This attribute can also be specified for individual hot spot elements.

Page 58: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 58

A web page with an image button that displays a calendar

The web page with the calendar displayed

Page 59: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 59

The aspx code for the calendar control <asp:Calendar ID="clnArrival" runat="server" Visible="False" OnSelectionChanged="clnArrival_SelectionChanged" BorderColor="Black" BorderStyle="Solid"> <TodayDayStyle BackColor="Blue" Font-Bold="True" ForeColor="White" /> <TitleStyle BackColor="Blue" Font-Bold="True" ForeColor="White" /> <NextPrevStyle ForeColor="White" /> </asp:Calendar>

Page 60: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 60

The SelectionChanged event procedure for the calendar control

protected void clnArrival_SelectionChanged(object sender, EventArgs e) { ddlMonth.SelectedValue = clnArrival.SelectedDate.Month.ToString(); ddlDay.SelectedValue = clnArrival.SelectedDate.Day.ToString(); clnArrival.Visible = false; ibtnCalendar.Visible = true; }

Page 61: Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1

Murach’s ASP.NET 4/C#, C6 © 2011, Mike Murach & Associates, Inc. Slide 61

Common properties of the Calendar class Property Description SelectionMode Specifies the type of selection that can be made.

Acceptable values are Day, DayWeek, DayWeekMonth, and None.

SelectedDate The currently selected date if a single date was selected, or the first selected date if multiple dates were selected.

SelectedDates A collection that contains all of the selected dates in sequence. To determine the number of dates that were selected, use the Count property of this collection.