很久没有写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. Wix 安装部署教程(十一) ---QuickWix

    这次发布的是这两天做的一个WIX工具QuickWIX,主要解决两个问题点1.对大文件快速生成wix标签(files,Directories,ComponentRef):2.比较前后两次工程的差异.大的 ...

  2. Web前端技术研究:Css hack技术---令人沮丧的技术

    我最近想好好整理下csshack技术,但是结果很沮丧,下面我将我最初写的笔记和大家分享下. 我在单位整理的研究笔记: 不同的浏览器对某些CSS代码解析会存在一定的差异,因此就会导致不同浏览器下给用户展 ...

  3. 让gcc支持成员函数模板的trick

    让gcc支持成员函数模板的trick 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循“署名-非商业用途-保持一致”创作公用协议   gcc 4.7.3 不支持成员 ...

  4. 如何将Icon转成Bitmap

    最近工作中有个需求是将Icon转成带Alpha通道的Bitmap, 虽然网上有不少这方面的文章,但很多都是错的, 这里记录下,或许对后来人有用. 要实现这个功能,我们首先需要理解Icon的格式,我们可 ...

  5. MongoDB官网驱动仓库封装

    定义IMongoRepositoryBase接口 public interface IMongoRepositoryBase     {         /// <summary>     ...

  6. Log4Net指南

    英文好的直接看这里:http://www.codeproject.com/Articles/140911/log4net-Tutorial 介绍 log4net.是.NET下面最伟大的日志工具之一.简 ...

  7. Redis学习笔记~分布式的Pub/Sub模式

    回到目录 redis的客户端有很多,这次用它的pub/sub发布与订阅我选择了StackExchange.Redis,发布与订阅大家应该很清楚了,首先一个订阅者,订阅一个服务,服务执行一些处理程序(可 ...

  8. MVVM架构~Knockoutjs系列之text,value,attr,visible,with的数据绑定

    返回目录 Knockoutjs是微软mvc4里一个新东西,用这在MVC环境里实现MVVM,小微这次没有大张旗鼓,而是愉愉的为我们开发者嵌入了一个实现MVVM的插件,这下面的几篇文章中,我和大家将一起去 ...

  9. fir.im Weekly - 深度揭秘 App 启动全过程

    世纪寒潮席卷全中国,可谓普天之下莫低0℃.无论怎样的严寒都抵挡不了程序员们的创造的激情... 本期的 fir.im Weekly ,最新的 iOS/Android 开发资源,GitHub 源码.前端技 ...

  10. Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生

    同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...