两个dropDownList和一个GridView的选择与显示
很久没有写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>
<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的选择与显示的更多相关文章
- GridView中两个DropDownList联动
GridView中两个DropDownList联动 http://www.cnblogs.com/qfb620/archive/2011/05/25/2057163.html Html: <as ...
- MVC编辑状态两个DropDownList联动
前几天使用jQuery在MVC应用程序中,实现了<jQuery实现两个DropDownList联动(MVC)>http://www.cnblogs.com/insus/p/3414480. ...
- jQuery实现两个DropDownList联动(MVC)
近段时间原本是学习MVC的,谁知道把jQuery也学上了.而且觉得对jQuery更感兴趣,比如今早上有写了一个练习<jQuery实现DropDownList(MVC)>http://www ...
- JS弄ASP.NET(C#)在页GridView信息选择行
做web发展还是新手我,为了之前获得Gridview中间值,它是通过服务器端控件通过第一Gridview将数据保存到服务器,当一个服务器,然后绑定的隐藏字段,在通过的js阅读隐藏字段值,如今,这种方法 ...
- 实现两个sym转一个sym
CVO输出如果是一个像素并行输出,选择内嵌人插入同步码.如果两个像素并行输出是不能选择内嵌的,只能选择分离的方式.如果把输出的并行数据给VIP并且要求是内嵌,那只能在内部转或者外部转. 这里是实现外部 ...
- 如何让hudson的两个job共用一个svn工作目录
作者:朱金灿 来源:http://blog.csdn.net/clever101 现在我的需求是这样的:一个软件需要编译完全版本和基础版本,完全版本的基础功能较多,基础版本只包含了基础功能.有时只需要 ...
- 在页面中添加两个 <select> 标签,用来显示年份和月份;同时添加两个 <ul> 标签,一个用来显示星期,另一个用来显示日期 在 JavaScript 脚本中动态添加年份和月份,获取当前日期的年份
查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的 Date 对象,在页面上显示一个万年历.选择不同的年份和月份,在页面中显示当前月的日历 实现思路: 在页面中添加两个 <s ...
- zeromq中两个dealer 通过一个router进行通信
发现有童鞋不是很清楚ZMQ中的“请求-回复”模式中的ROUTER怎么用,所以简单介绍一下“请求-回复”模式的使用(最后付代码). 一.讲一讲 1.要使用zmq 通过一个router进行通信,你首先需要 ...
- C# 图片的裁剪,两个图片合成一个图片
图片的裁剪,两个图片合成一个图片(这是从网上摘的) /// <summary> /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_ne ...
随机推荐
- Qt 设置应用程序图标
Qt4设置应用程序图标 将一个ico图标放在资源文件夹下; 然后建立txt,输入 IDI_ICON1 DISCARABLE "myico.ico"; 保存文件,将其后缀改为.rc; ...
- git 仓库、分支的区别
首先,要明白仓库的概念 仓库可以理解为repository, 就是存放代码的地方,—— 其实是一个比较笼统的概念,不管里面的内容,总之存放各种资源(代码.图片等等) 对于git应用开发者而已,本地一般 ...
- 使用Vue.js时,对Chrome控制台的一点小心得
之前对Chrome控制台的console.log()输出没太放心上,其实仔细研究后,对工作效率有显著的提示.看下面的五段代码: console.log(''); console.log(typeof ...
- xamarin UWP图片读取
xamarin使用listview时经常会使用图片,而图片的图片源设置在uwp平台中是有平台特性的,这点请注意区分处理.也就是说在android和IOS上正常的处理在uwp下却是错误的方式. 如同一张 ...
- Node.js与Sails~Model和ORM的持久化
回到目录 上一讲说了在sails里定义model及相关参数的说明,这一讲主要说一下如何将你的Model持久化到文件,关系数据库和Nosql数据库里,在持久化这点上,sails是统一管理的,它可以在/c ...
- sql基础知识:分页+排序
Oracle的分页还真是挺恶心地,不像mysql直接Limit就搞定 select name from student limit 0,20; Oracle需要借助rownum实现: select * ...
- Atitit 全屏模式的cs桌面客户端软件gui h5解决方案 Kiosk模式
Atitit 全屏模式的cs桌面客户端软件gui h5解决方案 Kiosk模式 1.1. Kiosk Software广泛用于公共电脑或者嵌入系统,最常用的就是ATM机.自动服务机之类的系统了.,1 ...
- JS---DOM操作有哪一些
一 DOM对象有哪一些 1 windos 1.属性 opener 2.方法 open(),close() 例:<script langguage="javascript&qu ...
- Android笔记——Android五大布局
一.五大布局 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是Li ...
- Python装饰器详解
python中的装饰器是一个用得非常多的东西,我们可以把一些特定的方法.通用的方法写成一个个装饰器,这就为调用这些方法提供一个非常大的便利,如此提高我们代码的可读性以及简洁性,以及可扩展性. 在学习p ...