DropdownList Chain within FormView
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropdownListChain.aspx.cs"
Inherits="local_DropdownListChain" %>
<!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>DropdownList Chain within FormView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="InfoID" DataSourceID="SqlDataSource4"
DefaultMode="Edit">
<EditItemTemplate>
<div>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Class1ID") %>' />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Class2ID") %>' />
<asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("Class3ID") %>' />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Class1Name" DataValueField="Class1ID" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
OnDataBound="DropDownList_DataBound" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
DataTextField="Class2Name" DataValueField="Class2ID" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
OnDataBound="DropDownList_DataBound" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3"
DataTextField="Class3Name" DataValueField="Class3ID" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
OnDataBound="DropDownList_DataBound" AutoPostBack="True">
</asp:DropDownList>
</div>
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="更新" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="取消" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:topicConnectionString %>"
SelectCommand="SELECT [Class1ID], [Class1Name] FROM [InfoClass1] ORDER BY [Class1ID]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:topicConnectionString %>"
SelectCommand="SELECT [Class2ID], [Class2Name] FROM [InfoClass2] WHERE ([Class1ID] = @Class1ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Class1ID" PropertyName="SelectedValue"
Type="Byte" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:topicConnectionString %>"
SelectCommand="SELECT [Class3ID], [Class3Name] FROM [InfoClass3] WHERE (([Class1ID] = @Class1ID) AND ([Class2ID] = @Class2ID))">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Class1ID" PropertyName="SelectedValue"
Type="Byte" />
<asp:ControlParameter ControlID="DropDownList2" Name="Class2ID" PropertyName="SelectedValue"
Type="Byte" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:topicConnectionString %>"
SelectCommand="SELECT * FROM [InfoMain] WHERE [infoID] = 153"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class local_DropdownListChain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
HiddenField field = list.Parent.FindControl("HiddenField1") as HiddenField;
field.Value = list.SelectedValue;
}
protected void DropDownList_DataBound(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
HiddenField field = list.Parent.FindControl("HiddenField1") as HiddenField;
list.SelectedValue = field.Value;
}
}