如何在FormView中实现DropDownList连动选择
来源:互联网 发布:网络违法营销行为 编辑:程序博客网 时间:2024/06/18 03:06
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906111015.aspx.cs" Inherits="DropDown_X200906111015" %><!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:FormView ID="FormView1" runat="server" AllowPaging="True" onpageindexchanging="FormView1_PageIndexChanging" DataKeyNames="ProductKey" > <ItemTemplate> ProductKey:<asp:Label ID="fProductKey" Text='<%#Eval("ProductKey")%>' runat="server"></asp:Label> <br /> Name: <asp:Label ID="fProductName" Text='<%#Eval("Name")%>' runat="server"></asp:Label> <br /> ProductClass: <asp:DropDownList ID="fProductClass" runat="server" AutoPostBack="True" OnInit="InitProductClass" OnSelectedIndexChanged="ProductClass_SelectedIndexChanged"> </asp:DropDownList> <br /> ProductSubClass: <asp:DropDownList ID="fProductSubClass" runat="server"> </asp:DropDownList> <br /> </ItemTemplate> </asp:FormView> </div> </form></body></html>
-- 后台文件
-------------------------------------------------------------------------------------------------------
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Drawing;/// <summary>/// 标题:FormView DropList 连动简单示例/// 日期:2009-06-11/// </summary>public partial class DropDown_X200906111015 : System.Web.UI.Page{ private DataTable Product { get { if (Session["Product"] == null) { DataTable fProduct = new DataTable(); fProduct.Columns.Add(new DataColumn("ProductKey", typeof(int))); fProduct.Columns.Add(new DataColumn("ProductClassKey", typeof(int))); fProduct.Columns.Add(new DataColumn("Name", typeof(string))); fProduct.Rows.Add(new object[] { 1, 1, "产品一" }); fProduct.Rows.Add(new object[] { 2, 2, "产品二" }); fProduct.Rows.Add(new object[] { 3, 3, "产品三" }); Session["Product"] = fProduct; } return (DataTable)Session["Product"]; } } private DataTable ProductClass { get { if (Session["ProductClass"] == null) { DataTable fProductClass = new DataTable(); fProductClass.Columns.Add(new DataColumn("ProductClassKey", typeof(int))); fProductClass.Columns.Add(new DataColumn("Name", typeof(string))); fProductClass.Rows.Add(new object[] { 1, "能源" }); fProductClass.Rows.Add(new object[] { 2, "办公" }); fProductClass.Rows.Add(new object[] { 3, "食品" }); Session["ProductClass"] = fProductClass; } return (DataTable)Session["ProductClass"]; } } private DataTable ProductSubClass { get { if (Session["ProductSubClass"] == null) { DataTable fProductSubClass = new DataTable(); fProductSubClass.Columns.Add(new DataColumn("ParentProductClassKey", typeof(int))); fProductSubClass.Columns.Add(new DataColumn("Name", typeof(string))); fProductSubClass.Rows.Add(new object[] { 1, "石油" }); fProductSubClass.Rows.Add(new object[] { 1, "煤" }); fProductSubClass.Rows.Add(new object[] { 2, "打印机" }); fProductSubClass.Rows.Add(new object[] { 2, "办公桌" }); fProductSubClass.Rows.Add(new object[] { 3, "朱奶" }); fProductSubClass.Rows.Add(new object[] { 3, "面包" }); Session["ProductSubClass"] = fProductSubClass; } return (DataTable)Session["ProductSubClass"]; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) this.BindFormView(); } private void BindFormView() { this.FormView1.DataSource = this.Product; this.FormView1.DataBind(); } protected void InitProductClass(object sender, EventArgs e) { DropDownList fDropDownList = (DropDownList)sender; FormViewRow fFormViewRow = (FormViewRow)fDropDownList.Parent.Parent; fDropDownList.DataTextField = "Name"; fDropDownList.DataValueField = "ProductClassKey"; fDropDownList.DataSource = this.ProductClass; fDropDownList.DataBind(); DataRowView fDataRowView = (DataRowView)this.FormView1.DataItem; if (fDataRowView != null) fDropDownList.SelectedValue = fDataRowView["ProductClassKey"].ToString(); } protected void ProductClass_SelectedIndexChanged(object sender, EventArgs e) { DropDownList fProductClassList = (DropDownList)sender; FormViewRow fFormViewRow = (FormViewRow)fProductClassList.Parent.Parent; DropDownList fProductSubClassList = (DropDownList)fFormViewRow.FindControl("fProductSubClass"); DataView fDataView = this.ProductSubClass.DefaultView; fDataView.RowFilter = string.Format("ParentProductClassKey = {0}", fProductClassList.SelectedValue); fProductSubClassList.DataTextField = "Name"; fProductSubClassList.DataValueField = "ParentProductClassKey"; fProductSubClassList.DataSource = fDataView; fProductSubClassList.DataBind(); } protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) { this.FormView1.PageIndex = e.NewPageIndex; this.BindFormView(); }}
- 如何在FormView中实现DropDownList连动选择
- DropDownList实现连动
- DropDownList连动菜单
- Javascript实现日期的连动选择
- Javascript实现日期的连动选择
- DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 如何实现在DataGrid中实现DropDownList控件的联动
- 在formview控件模板列中使用两个DropDownList控件进行联动时的操作方法
- 两个DropDownList在Repeater、DataList、DataGrid内的连动和冒泡事件(BubbleEvent)
- 实现省市连动
- 在DropDownList 中添加"请选择"
- 不能在 DropDownList 中选择多个项
- 不能在DropDownList 中选择多个项
- 不能在DropDownList 中选择多个项
- 不能在 DropDownList 中选择多个项
- 用递归在Dropdownlist中显示树状结构(连数据库)
- Dropdownlist中实现树行选择
- 在Struts中使用AjaxTags解决连动问题
- 多线程编程-线程的创建和终止
- linux /dev目录
- 法学专业缘何由香饽饽变成烫手的山芋
- jquery之利用ajax与服务器交谈(完全控制ajax请求之全局函数)
- 法律专业学生究竟可以从事哪些工作
- 如何在FormView中实现DropDownList连动选择
- Spring.NET学习笔记22——整合WCF(应用篇)
- 二排序数组中位数
- LINUX kernel编译
- Spring.NET学习笔记25——整合Quartz.NET(应用篇)
- Advanced SystemCare
- Spring.NET学习笔记26——整合NVelocity(应用篇)
- asp.net mvc 下使用 HttpResponse.Filter
- VPython - example - 模拟球在封闭箱子内的碰撞