<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<style>
body
{ font-family:verdana;
font-size:10pt;
}
</style>
<script runat="server">
Private listModels As DropDownList
Private listManufacturers As DropDownList
Private dataSourceModels As AccessDataSource
Protected Sub listManufacturers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
PopulateDependentDropDown()
End Sub
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
If FormView1.CurrentMode = FormViewMode.Edit Then
Dim dv As System.Data.DataRowView = FormView1.DataItem
listModels = FormView1.FindControl("listModels")
dataSourceModels = FormView1.FindControl("ModelsDataSource")
Dim m As String = dv("Manufacturer")
dataSourceModels.SelectParameters("Manufacturer").DefaultValue = m
listModels.DataBind()
If Not IsDBNull(dv("model")) Then
listModels.SelectedValue = dv("Model")
End If
End If
If FormView1.CurrentMode = FormViewMode.Insert Then
' Need to pre-populate the listModels control when the FormView control
' is switched into Insert mode. The listManufacturers list is populated
' automatically by the data source control, but the dependent
' dropdown is not and would otherwise remain blank.
PopulateDependentDropDown()
End If
End Sub
Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs)
listModels = FormView1.FindControl("listModels")
e.NewValues("Model") = listModels.SelectedValue
End Sub
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
listModels = FormView1.FindControl("listModels")
e.Values("Model") = listModels.SelectedValue
End Sub
Protected Sub PopulateDependentDropDown()
' Called in two circumstances -- a) when the selection in the master
' dropdown is changed. b) when a new (blank) record is displayed
' in insert mode. In the second case, The listManufacturers list is populated
' automatically by the data source control, but the dependent
' dropdown is not.
dataSourceModels = FormView1.FindControl("ModelsDataSource")
listManufacturers = FormView1.FindControl("listManufacturers")
listModels = FormView1.FindControl("listModels")
dataSourceModels.SelectParameters(0).DefaultValue = listManufacturers.SelectedValue
listModels.DataBind()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cascading DropDownList Controls in a FormView Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>
Dynamic Cascading Drop-Down Lists<br />
</h3>
<br />
This page shows two dependent drop-down lists in a FormView control template. The
lists appear when you edit or insert a record.<br />
<br />
The first list (manufacturer) is created and data-bound entirely in declarative
code. The second list (model) is bound in code based on the selection in the first
list. Code is required:<br />
<ol>
<li>To pass a parameter to the data source control and bind the list.</li>
<li>To pass the selected value to the FormView control during an update.</li>
<li>To repopulate the second list when the user makes a new selection in the first list.<br />
</li>
</ol>
<br />
<asp:FormView ID="FormView1"
runat="server"
AllowPaging="True"
DataKeyNames="CustomerID"
DataSourceID="CustomersDataSource"
OnDataBound="FormView1_DataBound"
OnItemUpdating="FormView1_ItemUpdating"
BackColor="#CCCCCC"
BorderColor="#999999"
BorderStyle="Solid"
BorderWidth="3px"
CellPadding="4"
CellSpacing="2"
ForeColor="Black"
GridLines="Both" OnItemInserting="FormView1_ItemInserting" >
<FooterStyle BackColor="#CCCCCC" />
<EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
Manufacturer:
<asp:Label ID="ManufacturerLabel" runat="server" Text='<%# Eval("Manufacturer") %>' />
<br />
Model:
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>' />
<br />
<br />
<asp:LinkButton ID="Edit"
runat="server"
CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="New"
runat="server"
CommandName="New">New</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
CustomerID:
<asp:Label ID="CustomerIDLabel1"
runat="server"
Text='<%# Eval("CustomerID") %>' />
<br />
Name:
<asp:TextBox ID="NameTextBox"
runat="server"
Text='<%# Bind("Name") %>' />
<br />
Manufacturer:
<asp:DropDownList
ID="listManufacturers"
runat="server"
AutoPostBack="True"
DataTextField="Manufacturer"
DataValueField="Manufacturer"
SelectedValue='<%# Bind("Manufacturer") %>'
DataSourceID="ManufacturersDataSource"
OnSelectedIndexChanged="listManufacturers_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource
ID="ManufacturersDataSource"
runat="server"
DataFile="~/App_Data/cars.mdb"
SelectCommand="SELECT [Manufacturer] FROM [Manufacturer] ORDER BY [Manufacturer]">
</asp:AccessDataSource>
<br />
Model:
<asp:DropDownList
ID="listModels"
runat="server"
DataSourceID="ModelsDataSource"
DataTextField="Model"
DataValueField="Model">
</asp:DropDownList>
<br />
<asp:AccessDataSource
ID="ModelsDataSource"
runat="server"
DataFile="~/App_Data/cars.mdb"
SelectCommand="SELECT * FROM [Models] where Manufacturer = ?">
<SelectParameters>
<asp:Parameter Name="Manufacturer" />
</SelectParameters>
</asp:AccessDataSource>
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Name:
<asp:TextBox ID="textName" runat="server" Text='<%# Bind("Name") %>' />
Manufacturer:
<asp:DropDownList
ID="listManufacturers"
runat="server"
AutoPostBack="True"
DataTextField="Manufacturer"
DataValueField="Manufacturer"
SelectedValue='<%# Bind("Manufacturer") %>'
DataSourceID="ManufacturersDataSource"
OnSelectedIndexChanged="listManufacturers_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource
ID="ManufacturersDataSource"
runat="server"
DataFile="~/App_Data/cars.mdb"
SelectCommand="SELECT [Manufacturer] FROM [Manufacturer] ORDER BY [Manufacturer]">
</asp:AccessDataSource>
<br />
Model:
<asp:DropDownList
ID="listModels"
runat="server"
DataSourceID="ModelsDataSource"
DataTextField="Model"
DataValueField="Model"
>
</asp:DropDownList>
<br />
<asp:AccessDataSource
ID="ModelsDataSource"
runat="server"
DataFile="~/App_Data/cars.mdb"
SelectCommand="SELECT * FROM [Models] where Manufacturer = ?">
<SelectParameters>
<asp:Parameter Name="Manufacturer" />
</SelectParameters>
</asp:AccessDataSource>
<br /><br />
<asp:LinkButton ID="UpdateButton"
RunAt="server"
Text="Save"
CommandName="Insert" />
<asp:LinkButton ID="CancelUpdateButton"
RunAt="server"
Text="Cancel"
CommandName="Cancel" />
</InsertItemTemplate>
<InsertRowStyle BackColor="#FFE0C0" />
</asp:FormView>
<asp:AccessDataSource ID="CustomersDataSource"
runat="server"
DataFile="~/App_Data/cars.mdb"
SelectCommand="SELECT [CustomerID], [Name], [Manufacturer], [Model] FROM [Customers]"
UpdateCommand="UPDATE [Customers] SET [Name] = ?, [Manufacturer] = ?, [Model] = ? WHERE [CustomerID] = ?"
InsertCommand="Insert Into [Customers] ([Name], [Manufacturer], [Model]) Values (?, ?, ?)">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Manufacturer" Type="String" />
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="CustomerID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Manufacturer" Type="String" />
<asp:Parameter Name="Model" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
Colorized by: CarlosAg.CodeColorizer