<%@ 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">
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dsc As AccessDataSource = FormView1.FindControl("ModelsDataSource")
Dim ddl1 As DropDownList = FormView1.FindControl("DropDownList1")
Dim ddl2 As DropDownList = FormView1.FindControl("DropDownList2")
dsc.SelectParameters(0).DefaultValue = ddl1.SelectedValue
ddl2.DataBind()
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
Dim ddl2 As DropDownList = FormView1.FindControl("DropDownList2")
Dim dsc As AccessDataSource = FormView1.FindControl("ModelsDataSource")
Dim m As String = dv("Manufacturer")
dsc.SelectParameters("Manufacturer").DefaultValue = m
ddl2.DataBind()
If Not IsDBNull(dv("model")) Then
ddl2.SelectedValue = dv("Model")
End If
End If
End Sub
Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs)
Dim ddl2 As DropDownList = FormView1.FindControl("DropDownList2")
e.NewValues("Model") = ddl2.SelectedValue
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>
<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="CustomerID"
DataSourceID="CustomersDataSource" OnDataBound="FormView1_DataBound" OnItemUpdating="FormView1_ItemUpdating">
<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="DropDownList1" runat="server" AutoPostBack="True" DataTextField="Manufacturer"
DataValueField="Manufacturer" Width="122px" SelectedValue='<%# Bind("Manufacturer") %>'
DataSourceID="ManufacturersDataSource" OnSelectedIndexChanged="DropDownList1_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="DropDownList2" 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>
<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>
</ItemTemplate>
</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] = ?">
<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>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
Colorized by: CarlosAg.CodeColorizer