Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.

Slides:



Advertisements
Similar presentations
1111 Creating ASPX Controls Programatically Objectives You will be able to Dynamically add controls to a page. Dynamically alter properties of controls.
Advertisements

 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
11 User Controls II Chapter Objectives You will be able to Create a realistic reusable user control. Use data binding in a user control. Change.
11 ASP.NET Controls II Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Asp.NET Core Server Controls. Slide 2 Lecture Overview Understanding the types of ASP.NET controls HTML controls ASP.NET (Web) controls.
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 1: An Introduction To ASP.NET Web Programming
An Introduction to ASP.NET Web Pages 2 Module 1: Webmatrix Installation and Your First Web Site Tom Perkins.
11 ASP.NET Controls Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
ASP.NET Programming with C# and SQL Server First Edition
Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 8: WebForms — Web-based.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
JavaScript & jQuery the missing manual Chapter 11
1 Web-Enabled Decision Support Systems Introduction to ASP.NET Prof. Name Position (123) University Name.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
ASP.NET Part 4 Instructor: Charles Moen CSCI/CINF 4230.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
11 Updating a Database Table Textbook Chapter 14.
Creating Web Applications Using ASP.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 Project 4: XML Product Browser (Not Graded). Objectives This project is an exercise of the following knowledge and skills: Accessing and displaying.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
OBJECT REFERENCE. Objective How to work with objects in SharePoint by using classes?
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Internet Technologies and Web Application Web Services With ASP.NET Tutorial: Introduction to.
File I/O 11_file_processing.ppt
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
C# Tutorial -1 ASP.NET Web Application with Visual Studio 2005.
11 Using ADO.NET II Textbook Chapter Getting Started Last class we started a simple example of using ADO.NET operations to access the Addresses.
Caching Chapter 12. Caching For high-performance apps Caching: storing frequently-used items in memory –Accessed more quickly Cached Web Form bypasses:
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Meridium EPiServer Premium Partner EPiMore Partner EPiServer is a major focus area Founded in employees 2.
Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
ASP.NET 4 Unleashed Chapter 1. .aspx page: contains C# script and HTML code including tags. Listing 1.1 FirstPage.aspx.
Christopher M. Pascucci.NET Programming: WebForm Events.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Web Development in Microsoft Visual Studio 2013 / 2015.
11 ASP.NET Server Controls Beginning ASP.NET in C# and VB Chapter 4.
1111 Creating ASPX Controls Programatically Objectives You will be able to Dynamically add controls to a page. Dynamically alter properties of controls.
11 Getting Started with ASP.NET Beginning ASP.NET in C# and VB Chapters 1 and 2.
1111 Creating HTML Programatically Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML.
1 Project 4 Address Lookup. Project 4 Write an ASP.NET app that permits users to retrieve addresses from a potentially large list of addresses. There.
1 Windows Forms II Chapter RadioButton / GroupBox Controls Used to solicit a multiple choice input. Radio buttons work as a group. Selecting one.
1111 Master Pages Beginning ASP.NET in C# and VB Chapter 6.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
1 Working with Controls at Run Time. 2 Objectives You will be able to Add controls to a Windows form at run time. Modify controls at run time.
Creating a Web Site Creating a new Web site Defining and using folders Creating and editing Web pages Viewing pages in a Web browser.
Beginning ASP.NET in C# and VB Chapter 9
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
ASP.NET Part 2 Instructor: Charles Moen CSCI/CINF 4230.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
ASP.NET Forms.
Allowing File Uploads.
CS 3870 Prog6 Roles Management Due Monday, November 5 Group Assignment.
Web Development in Microsoft Visual Studio 2013
CS 3870/CS 5870 Web User Controls Events (II).
ASP.NET.
5/6/2019 Session 8.2 Postback, ViewState
Allowing File Uploads.
Presentation transcript:

Dynamic Dropdown Lists 1

Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown List at run time using data from a file. 2

DropDownList The ASPX DropDownList generates an HTML Select control. A good way to get a user input from an extensive set of discrete choices. The list should not be too long. If there are many choices divide it into subsets. Use one DropDownList to select the subset. Use a second DropDownList to select from the subset. Second DropDownList must be dynamically populated based on choice from the first. 3

Example Select a city from a list of 275 largest US cities and display its population. Let first DropDownList select a state. Second DropDownList includes just the cities in that state. 4

Largest US Cities 5

Data File Download to desktop: File cities.csv 6

7 Dropdown List Demo Create a new C# ASP.NET empty web site. Name: Dropdown_List_Demo

Add Default.aspx 8

9

Add New Folder 10

New Folder for the Data File In Visual Studio Rename the new folder App_Data. Note: This name is significant. Open the App_Data folder in the website folder and copy the downloaded file cities.csv into it. 11

The App_Data Folder 12

Add Data File to the Project Back in Visual Studio, right click on App_Data and add the file cities.csv to the project. 13

Add cities.csv to the Project Select “All Files”

cities.csv in the Project 15

16 Class City Let’s create a class to hold information about a city. An Entity class Frequently used to create objects corresponding to information in a database. Object-Relational Mapping Automated tools are available Best to do manually until you have a thorough understanding. (This is an especially simple example.)

17 Class City

Add Class City 18

Add Class City 19

Class City using System; public class City : IComparable { public int rank; public String name; public String state; public int population; public City(string line) { string[] info; info = line.Split(','); rank = int.Parse(info[0]); name = info[1]; state = info[2]; population = int.Parse(info[3]); } public int CompareTo(City other) { int result = string.Compare(this.state, other.state); if (result == 0) { result = string.Compare(this.name, other.name); } return result; } } 20 In order for us to use the sort method for List, the City class must implement the IComparable interface and provide a CompareTo method.

Design the Page 21 ddlState ddlCity

Read the Data On Page_Load Read the file cities.csv Create a City object from each line. Add the City objects to a list. Sort the list by state name then city name. Save the list as an Application variable. Populate the State Dropdown List. It will never change. 22

Default.aspx.cs using System; using System.IO; using System.Collections.Generic; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { List cities = new List (); 23

Default.aspx.cs protected void Page_Load(object sender, EventArgs e) { if (Application["cities"] == null) { string path = Server.MapPath("App_Data/cities.csv"); using (StreamReader readFile = new StreamReader(path)) { string line; while ((line = readFile.ReadLine()) != null) { City city = new City(line); cities.Add(city); } } cities.Sort(); Application["cities"] = cities; } else { cities = (List )Application["cities"]; } 24

Default.aspx.cs (continued) if (IsPostBack) { return; } // Initial page load only ListItem li = new ListItem("", ""); ddlState.Items.Add(li); string prev_state = ""; foreach (City city in cities) { if (city.state != prev_state) { // Add state to dropdown list. li = new ListItem(city.state, city.state); ddlState.Items.Add(li); prev_state = city.state; } } 25 Build and run.

Dropdown_List_Demo in Chrome 26 Note scrollbar on Dropdown List

Add Event Handler When the user selects a state, populate the City list with cities in that state. Need an event handler for Selected Index Changed. Double click on the States Dropdown List in the designer. Visual Studio adds event handler to Default.aspx.cs 27

SelectedIndexChanged Event Handler Start with a stub. protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { string selected_state = ddlState.SelectedValue; lblPopulation.Text = "You selected " + selected_state; } Try it! 28

Page in Chrome 29 State selected. Nothing happens! What’s wrong?

Nothing Happens There is no postback when an item is selected from ddlState. We need AutoPostback. 30

Set AutoPostBack 31 Try again

Works as Expected 32 Add code to populate the City DropDownList.

Populate ddlCity protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { string selected_state = ddlState.SelectedValue; ddlCity.Items.Clear(); ListItem li = new ListItem("", ""); ddlCity.Items.Add(li); foreach (City city in cities) { if (city.state == selected_state) { li = new ListItem(city.name, city.name); ddlCity.Items.Add(li); } } } 33

ddlCity Populated 34 Add a SelectedIndexChanged event handler for ddlCity and set AutoPostBack.

ddlCity Event Handler protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e) { int population = 0; foreach (City city in cities) { if (city.name == ddlCity.SelectedValue) { population = city.population; break; } } lblPopulation.Text = "Population of " + ddlCity.SelectedValue + " is " + population.ToString(); } 35 Try it!

Select Tampa 36

Result 37

A Rough Edge When we select a new state, the previous city population stays on the page until we select a new city. Solution: Clear the label on SelectedIndexChanged. 38

A Minor Bug What happens if user selects the blank entry at the beginning of the list? (after selecting a nonblank entry) 39

A Minor Bug 40

Solution Check for this and ignore the event. protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { lblPopulation.Text = ""; if (ddlState.SelectedIndex == 0) { return; }... protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e) { lblPopulation.Text = ""; if (ddlCity.SelectedIndex == 0) { return; }... 41

Works as Expected 42 Blank initial item selected.

Summary We can populate a DropDownList with information determed at run time. Contents of a file. Information determined by user input. If we need an immediate action when the user selects an item, set AutoPostBack to true. End of Presentation 43