很久没有写ASP.NET了,今天有看到论坛上一个问题:
"两个dropDownList和一个GridView,已经进行了数据绑定,现在想让第一个下拉菜单的数据改变时,第二个下拉菜单自动变到相应的数据,同时选中gridview中相对应的行,不知道如何实现,很急,求大神相助"

其实,实现起来算得上简单,下面先从准备数据开始,创建一个对象Customer:

source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Summary description for Customer
/// </summary>
namespace Insus.NET
{
public class Customer
{
private int _CustomerID;
private string _CustomerName;
private string _PID;
private bool _IsActived; public int CustomerID
{
get { return _CustomerID; }
set { _CustomerID = value; }
}
public string CustomerName
{
get { return _CustomerName; }
set { _CustomerName = value; }
}
public string PID
{
get { return _PID; }
set { _PID = value; }
}
public bool IsActived
{
get { return _IsActived; }
set { _IsActived = value; }
}
}
}

接下来,我们创建一个实具,数据来源全在此类实现 CustomerEntity:

source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Summary description for CustomerEntity
/// </summary>
namespace Insus.NET
{
public class CustomerEntity
{
public CustomerEntity()
{
//
// TODO: Add constructor logic here
//
} public IEnumerable<Customer> Customers = new List<Customer> {
new Customer {CustomerID = ,CustomerName = "张三",PID = "" },
new Customer {CustomerID = ,CustomerName = "李四",PID = "" },
new Customer {CustomerID = ,CustomerName = "吴广",PID = "" },
new Customer {CustomerID = ,CustomerName = "王维",PID = "" },
new Customer {CustomerID = ,CustomerName = "赵勇",PID = "" }
}; public IEnumerable<Customer> GetForFirstDropDownData()
{
var oo = Customers.Select(o => new Customer { CustomerID = o.CustomerID, CustomerName = o.CustomerName });
return oo;
} public IEnumerable<Customer> GetForSecondDropDownData(int customerId)
{
var oo = Customers.Select(o => new Customer { CustomerID = o.CustomerID, PID = o.PID })
.Where(w => w.CustomerID == customerId);
return oo;
} public IEnumerable<Customer> GetForGridview(int customerId)
{
List<Customer> _cust = new List<Customer>();
foreach (Customer c in Customers)
{
if (c.CustomerID == customerId)
_cust.Add(new Customer { CustomerID = c.CustomerID, CustomerName = c.CustomerName, PID = c.PID, IsActived = true });
else
_cust.Add(c);
}
return _cust;
} }
}

Ok,数据准备好了,下面就可以创建网页实现相关的功能xxx.aspx:

source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<%# Eval("CustomerID") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerName">
<ItemTemplate>
<%# Eval("CustomerName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PID">
<ItemTemplate>
<%# Eval("PID") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

xxx.aspx.cs:

source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
using System.Data; public partial class _Default : System.Web.UI.Page
{
CustomerEntity ce = new CustomerEntity(); protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
} private void Data_Binding()
{
this.DropDownList1.DataSource = ce.GetForFirstDropDownData();
this.DropDownList1.DataValueField = "CustomerID";
this.DropDownList1.DataTextField = "CustomerName";
this.DropDownList1.DataBind(); this.DropDownList2.DataSource = ce.GetForSecondDropDownData(Convert.ToInt32(this.DropDownList1.SelectedItem.Value));
this.DropDownList2.DataTextField = "PID";
this.DropDownList2.DataBind(); this.GridView1.DataSource = ce.GetForGridview(Convert.ToInt32(this.DropDownList1.SelectedItem.Value));
this.GridView1.DataBind();
} protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
this.DropDownList2.DataSource = ce.GetForSecondDropDownData(Convert.ToInt32(ddl.SelectedItem.Value));
this.DropDownList2.DataTextField = "PID";
this.DropDownList2.DataBind(); this.GridView1.DataSource = ce.GetForGridview(Convert.ToInt32(ddl.SelectedItem.Value));
this.GridView1.DataBind();
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow) return; Customer drv = (Customer)e.Row.DataItem; bool isActived = (bool)drv.IsActived; if (isActived)
e.Row.BackColor = System.Drawing.Color.Red;
}
}

动画实时演示:

两个dropDownList和一个GridView的选择与显示的更多相关文章

  1. GridView中两个DropDownList联动

    GridView中两个DropDownList联动 http://www.cnblogs.com/qfb620/archive/2011/05/25/2057163.html Html: <as ...

  2. MVC编辑状态两个DropDownList联动

    前几天使用jQuery在MVC应用程序中,实现了<jQuery实现两个DropDownList联动(MVC)>http://www.cnblogs.com/insus/p/3414480. ...

  3. jQuery实现两个DropDownList联动(MVC)

    近段时间原本是学习MVC的,谁知道把jQuery也学上了.而且觉得对jQuery更感兴趣,比如今早上有写了一个练习<jQuery实现DropDownList(MVC)>http://www ...

  4. JS弄ASP.NET(C#)在页GridView信息选择行

    做web发展还是新手我,为了之前获得Gridview中间值,它是通过服务器端控件通过第一Gridview将数据保存到服务器,当一个服务器,然后绑定的隐藏字段,在通过的js阅读隐藏字段值,如今,这种方法 ...

  5. 实现两个sym转一个sym

    CVO输出如果是一个像素并行输出,选择内嵌人插入同步码.如果两个像素并行输出是不能选择内嵌的,只能选择分离的方式.如果把输出的并行数据给VIP并且要求是内嵌,那只能在内部转或者外部转. 这里是实现外部 ...

  6. 如何让hudson的两个job共用一个svn工作目录

    作者:朱金灿 来源:http://blog.csdn.net/clever101 现在我的需求是这样的:一个软件需要编译完全版本和基础版本,完全版本的基础功能较多,基础版本只包含了基础功能.有时只需要 ...

  7. 在页面中添加两个 <select> 标签,用来显示年份和月份;同时添加两个 <ul> 标签,一个用来显示星期,另一个用来显示日期 在 JavaScript 脚本中动态添加年份和月份,获取当前日期的年份

    查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的 Date 对象,在页面上显示一个万年历.选择不同的年份和月份,在页面中显示当前月的日历 实现思路: 在页面中添加两个 <s ...

  8. zeromq中两个dealer 通过一个router进行通信

    发现有童鞋不是很清楚ZMQ中的“请求-回复”模式中的ROUTER怎么用,所以简单介绍一下“请求-回复”模式的使用(最后付代码). 一.讲一讲 1.要使用zmq 通过一个router进行通信,你首先需要 ...

  9. C# 图片的裁剪,两个图片合成一个图片

    图片的裁剪,两个图片合成一个图片(这是从网上摘的) /// <summary>         /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png  新图1_ne ...

随机推荐

  1. silverlight中Combox绑定数据以及动态绑定默认选定项的用法

    在Sliverlight中,经常要用到下拉框Combox,然而Combox的数据绑定却是一件令初学者很头疼的事情.今天就来总结一下下拉框的使用方法: 下面写一个简单的例子吧.先写一个日期的Model, ...

  2. java提高篇(十六)-----异常(一)

         Java的基本理念是“结构不佳的代码不能运行”!!!!! 大成若缺,其用不弊. 大盈若冲,其用不穷. 在这个世界不可能存在完美的东西,不管完美的思维有多么缜密,细心,我们都不可能考虑所有的因 ...

  3. 两个Fragment之间如何传递数据

    FragmentA启动FragmentB,做一些选择操作后,返回FragmentA,需要把FragmentB里面选择的数据传回来.有什么办法? Fragment之间不能直接通信,必须通过Activit ...

  4. Excel批量插入多行

    在一个表中插入10行空行,你是不是这样做的:选取要插入的位置 - 右键插入行.重复插入10次.如下图所示: 其实插入多行是可以一次完成的,方法是:你需要插入多少行,就向下选取多少行再插入.如下图所示:

  5. sql基础知识:日期的常用用法

    日期操作 select sysdate,add_months(sysdate,12) from dual; -- + 1 year select sysdate,add_months(sysdate, ...

  6. Microsoft Visual Studio正忙解决办法

    问题描述 前段时间用vs2015进行开发.出现如下问题,关闭vs进程重启vs还是无法解决. 如何解决 进入本地项目.vs文件夹 这个文件夹下有个.suo文件,删除该文件,用任务管理器杀掉vs的进程,重 ...

  7. javascript类型系统——Number数字类型

    × 目录 [1]定义 [2]整数 [3]浮点数[4]科学记数[5]数值精度[6]数值范围[7]特殊数值[8]转成数值[9]实例方法 前面的话 javascript只有一个数字类型,它在内部被表示为64 ...

  8. MemCache分布式缓存的一个bug

    Memcached分布式缓存策略不是由服务器端至支持的,多台服务器之间并不知道彼此的存在.分布式的实现是由客户端代码(Memcached.ClientLibrary)通过缓存key-server映射来 ...

  9. 牛顿法与拟牛顿法学习笔记(五)L-BFGS 算法

    机器学习算法中经常碰到非线性优化问题,如 Sparse Filtering 算法,其主要工作在于求解一个非线性极小化问题.在具体实现中,大多调用的是成熟的软件包做支撑,其中最常用的一个算法是 L-BF ...

  10. Windows 8(虚拟机环境)安装.NET Framework3.5(includes .NET 2.0 and 3.0)

    按照这篇文章:http://blogs.technet.com/b/aviraj/archive/2012/08/04/windows-8-enable-net-framework-3-5-inclu ...