| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions! Dokkio, a new product from the PBworks team, integrates and organizes your Drive, Dropbox, Box, Slack and Gmail files. Sign up for free.

View
 

Using DropDownLists with Postback

Page history last edited by PBworks 9 years, 1 month ago

Using a DropDownList with Postback

 

 

Prerequisites

 

 

 

Description of Video

 

This lecture goes over how to create and use a dropdownlist and have it trigger the contents of a second dropdownlist. In addition it demonstrates how to create a DataTable in Code. Code for this example is in C#

 

Video 

 

Download Link (right click and save target as ...). 

 

 

Reference Materials

 

Default.aspx.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ComboBoxExample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlStates.AutoPostBack = true;
                ddlStates.DataSource = ResourceCommon.DataTableStates();
                ddlStates.DataTextField = ResourceCommon.DataTable_States_StateName;
                ddlStates.DataValueField = ResourceCommon.DataTable_States_StateAbbreviation;
                ddlStates.DataBind();
            }

        }

        protected void ddlStates_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCities.Visible = true;
            ddlCities.DataSource = ResourceCommon.DataTableCities(ddlStates.SelectedValue);
            ddlCities.DataTextField = "CityName";
            ddlCities.DataValueField = "CityName";
            ddlCities.DataBind();
        }
    }
}

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ComboBoxExample._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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList runat="server" ID = "ddlStates" 
            onselectedindexchanged="ddlStates_SelectedIndexChanged" ></asp:DropDownList><br />
            
        <asp:DropDownList runat="server" ID = "ddlCities" visible="false"></asp:DropDownList><br />

    </div>
    </form></body></html>

 

ResourceCommon.cs

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace ComboBoxExample
{
    public class ResourceCommon
    {
        public const String DataTable_States_StateName = "StateName";
        public const String DataTable_States_StateAbbreviation = "StateAbbreviation";

        public static DataTable DataTableStates()
        {
            DataTable dt = new DataTable("States");

            dt.Columns.Add("StateName", Type.GetType("System.String"));
            dt.Columns.Add("StateAbbreviation", Type.GetType("System.String"));

            FillRowState(dt, "Florida", "FL");
            FillRowState(dt, "Georgia", "GA");
            FillRowState(dt, "Alabama", "AL");

            return dt;
        }

        private static void FillRowState(DataTable dt, String StateName, String StateAbbreviation)
        {
            DataRow dr = dt.NewRow();

            dr["StateName"] = StateName;
            dr["StateAbbreviation"] = StateAbbreviation;

            dt.Rows.Add(dr);
        }

        public static DataTable DataTableCities(String StateAbbreviation)
        {
            DataTable dt = new DataTable("Cities");

            dt.Columns.Add("CityName", Type.GetType("System.String"));

            switch (StateAbbreviation)
            {
                case "FL":
                    FillRowCities(dt, "Jacksonville");
                    FillRowCities(dt, "Orlando");
                    FillRowCities(dt, "Miami");
                    FillRowCities(dt, "Tampa");
                    break;

                case "GA":
                    FillRowCities(dt, "Atlanta");
                    FillRowCities(dt, "Savannah");
                    break;
                
                case "AL":
                    FillRowCities(dt, "Birminingham");
                    FillRowCities(dt, "Huntsville");
                    break;
            }

            return dt;
        }

        private static void FillRowCities(DataTable dt, String CityName)
        {
            DataRow dr = dt.NewRow();

            dr["CityName"] = CityName;
            dt.Rows.Add(dr);
        }
    }
}

 

 

Additional Information

 

 

Comments (0)

You don't have permission to comment on this page.