+ All Categories
Home > Documents > Chapter 6 Slides ASP

Chapter 6 Slides ASP

Date post: 08-Feb-2016
Category:
Upload: smitanair143
View: 30 times
Download: 0 times
Share this document with a friend
Description:
asp.net
Popular Tags:
63
Murach’s ASP.NET 3.5/C#, C6 © 2008, Mike Murach & Associates, Inc. Slide 1 C hapter6 How to w ork w ith servercontrols
Transcript
Page 1: Chapter 6 Slides ASP

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

Chapter 6

How to work with server controls

Page 2: Chapter 6 Slides ASP

Murach’s ASP.NET 3.5/C#, C6 © 2008, 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: Chapter 6 Slides ASP

Murach’s ASP.NET 3.5/C#, C6 © 2008, 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: Chapter 6 Slides ASP

Murach’s ASP.NET 3.5/C#, C6 © 2008, 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: Chapter 6 Slides ASP

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

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

site

Login controls Controls that provide user authentication

WebParts controls Controls that let you create a page from user-selectable

components

Page 6: Chapter 6 Slides ASP

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

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

a postback

HTML controls Standard HTML controls that can be converted to HTML server

controls Not commonly used in ASP.NET applications

Page 7: Chapter 6 Slides ASP

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

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

Label lbl TextBox txt Button btn LinkButton lbtn ImageButton ibtn HyperLink hlnk DropDownList ddl ListBox lst

Page 8: Chapter 6 Slides ASP

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

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 9: Chapter 6 Slides ASP

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

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 10: Chapter 6 Slides ASP

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

The asp tags for two button controls that use the same event handler

<asp:Button id="btnPrevious" runat="server" Text="Previous" OnClick="NavigationButtons_Click" /> <asp:Button id="btnNext" runat="server" Text="Next" OnClick="NavigationButtons_Click" />

Page 11: Chapter 6 Slides ASP

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

Common control events Event Attribute Click OnClick Command OnCommand TextChanged OnTextChanged SelectedIndexChanged OnSelectedIndexChanged CheckedChanged OnCheckedChanged

Page 12: Chapter 6 Slides ASP

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

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

Page 13: Chapter 6 Slides ASP

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

The aspx code for the form <form id="form1" runat="server" defaultfocus="txtName" defaultbutton="btnNext"> <div> Please enter your contact information:<br /><br /> <table> <tr> <td style="width: 75px"> <span style="text-decoration: underline">N</span>ame:</td> <td style="width: 100px"> <asp:TextBox ID="txtName" runat="server" AccessKey="N"></asp:TextBox></td></tr> <tr> <td style="width: 75px"> <span style="text-decoration: underline">E</span>mail:</td> <td style="width: 100px"> <asp:TextBox ID="txtEmail" runat="server" AccessKey="E"> </asp:TextBox></td></tr></table><br />

Page 14: Chapter 6 Slides ASP

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

The aspx code for the form (continued) <asp:Button ID="btnPrevious" runat="server" AccessKey="P" Text="Previous" OnClick="btnPrevious_Click" />&nbsp; <asp:Button ID="btnNext" runat="server" AccessKey="N" Text="Next" OnClick="btnNext_Click" /><br /> </div> </form>

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

Page 15: Chapter 6 Slides ASP

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

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" Height="60px" Width="68px" PostBackUrl="~/Cart.aspx" />

Page 16: Chapter 6 Slides ASP

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

Common button attributes Text ImageUrl AlternateText CausesValidation CommandName CommandArgument PostBackUrl

Page 17: Chapter 6 Slides ASP

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

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 18: Chapter 6 Slides ASP

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

An image used for an image button control

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

Page 19: Chapter 6 Slides ASP

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

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 20: Chapter 6 Slides ASP

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

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 21: Chapter 6 Slides ASP

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

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 22: Chapter 6 Slides ASP

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

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 23: Chapter 6 Slides ASP

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

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 24: Chapter 6 Slides ASP

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

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 25: Chapter 6 Slides ASP

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

Common text box attributes TextMode Text MaxLength Wrap ReadOnly Columns Rows

Page 26: Chapter 6 Slides ASP

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

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 27: Chapter 6 Slides ASP

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

Four check boxes and two radio buttons displayed in a browser

Page 28: Chapter 6 Slides ASP

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

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 29: Chapter 6 Slides ASP

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

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 30: Chapter 6 Slides ASP

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

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 31: Chapter 6 Slides ASP

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

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 32: Chapter 6 Slides ASP

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

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 33: Chapter 6 Slides ASP

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

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 34: Chapter 6 Slides ASP

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

A drop-down list 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 35: Chapter 6 Slides ASP

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

Common properties of list box and drop-down list controls Items Rows SelectedItem SelectedIndex SelectedValue SelectionMode

Page 36: Chapter 6 Slides ASP

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

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 37: Chapter 6 Slides ASP

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

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 of a drop-down list protected void ddlDay_SelectedIndexChanged(object sender, EventArgs e) { int dayNumber = Convert.ToInt32(ddlDay.SelectedValue); }

Page 38: Chapter 6 Slides ASP

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

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 39: Chapter 6 Slides ASP

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

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 40: Chapter 6 Slides ASP

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

The ListItem Collection Editor dialog box

Page 41: Chapter 6 Slides ASP

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

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

Page 42: Chapter 6 Slides ASP

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

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 43: Chapter 6 Slides ASP

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

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 44: Chapter 6 Slides ASP

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

Attributes for formatting radio button and check box lists Attribute Description RepeatLayout Specifies whether ASP.NET should use table

tags (Table) 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 45: Chapter 6 Slides ASP

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

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 46: Chapter 6 Slides ASP

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

A bulleted list and a numbered list displayed in a browser

Page 47: Chapter 6 Slides ASP

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

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 48: Chapter 6 Slides ASP

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

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 49: Chapter 6 Slides ASP

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

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 (blstCategories.Items[0].Selected) ...

Page 50: Chapter 6 Slides ASP

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

Some of the Help documentation for the calendar control

Page 51: Chapter 6 Slides ASP

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

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 52: Chapter 6 Slides ASP

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

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. Width The width of the image. Height The height of the image.

Page 53: Chapter 6 Slides ASP

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

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 54: Chapter 6 Slides ASP

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

A file upload control displayed in a browser

The aspx code used to implement the file upload File upload:<br /><br /> <asp:FileUpload ID="FileUpload1" 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 55: Chapter 6 Slides ASP

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

The Click event handler 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 56: Chapter 6 Slides ASP

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

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 57: Chapter 6 Slides ASP

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

The California.gif image

The aspx code for the image map control <asp:ImageMap ID="imapCA" runat="server" ImageUrl="~/Images/California.GIF" HotSpotMode="PostBack" OnClick="imapCA_Click"> <asp:PolygonHotSpot Coordinates="76, 228, 177, 158, 121, 111, 121, 3, 0, 3, 0, 83, 76, 228" PostBackValue="North" /> <asp:PolygonHotSpot Coordinates="76, 229, 177, 159, 301, 275, 296, 347, 215, 358, 111, 295, 76, 229" PostBackValue="South" /> </asp:ImageMap>

Page 58: Chapter 6 Slides ASP

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

The Click event handler for the image map protected void imapCA_Click(object sender, ImageMapEventArgs e) { string region; if (e.PostBackValue.Equals("North")) region = "NorthernCalifornia"; else region = "SouthernCalifornia"; }

Page 59: Chapter 6 Slides ASP

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

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 60: Chapter 6 Slides ASP

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

A web page with an image button that displays a calendar

The web page with the calendar displayed

Page 61: Chapter 6 Slides ASP

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

The asp tag 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 62: Chapter 6 Slides ASP

Murach’s ASP.NET 3.5/C#, C6 © 2008, Mike Murach & Associates, Inc. Slide 62

The SelectionChanged event handler 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 63: Chapter 6 Slides ASP

Murach’s ASP.NET 3.5/C#, C6 © 2008, Mike Murach & Associates, Inc. Slide 63

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.


Recommended