- 热门文章:
- · KW大师精品文章赏析
- · 实现一个客户端的DataSet-----index.htm
- · 实现一个客户端的DataSet-----ClientDataSetDataProvider.asmx.cs(1)
- · 实现一个客户端的DataSet-----ClientDataSetDataProvider.asmx.cs(2)
- · 实现一个客户端的DataSet-----ClientDataSet.htc
- · 安装framework以后出现不能显示aspx页面,提示用户名和密码不匹配问题的解决(chicken修改补充)
- · IIS5 HTTP500内部错误解决办法(转自eNet)------(一)
- · IIS5 HTTP500内部错误解决办法(转自eNet)------(二)
- · IIS5 HTTP500内部错误解决办法(转自eNet)-------(三)
- · page_Load和page_Init的区别
- · 关于如何添加一个自增的列【原创】
- · 获得存储过程返回值的方法(return的值)
上一篇:web组件的通信---浅谈事件 >>
Using DropDownList control in DataGrid
By Eric Zheng
When I was developing a web application couple days ago, I found some interesting things about the datagrid, I want to share them with other vs.net programmers, so I wrote this article. This article will demonstrate how to use DropDownList control in datagrid.
The essential part of the DropDown.aspx file is the following: <asp:DropDownList id="DropDownList1" runat="server"DataSource="<%# GetCategory() %>"DataTextField="categoryname"DataValueField="categoryid"SelectedIndex=@#<%# GetCategoryID((string)DataBinder.Eval(Container.DataItem, "categoryname")) %>@# />In second line, we set the datasource of the dropdownlist control to a function @#GetCategory()@#, this function fetches the Category records from database and returns a datatable. In the last line, we set the SelectedIndex to a funciton @#GetCategoryID@#, this function takes the current Categoryname as its argument, and returns the locaiton(an integer) of the Categoryname, this enables the DorpDownList control to display the correct Categoryname for the current record.
The following is the C# code: using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.OleDb;using System.Configuration;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace Management{ public class DropDown : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid ProductGrid; protected DataTable _category; //new a database class to get records, ProductDb is a public class //containing several functions protected ProductDb pdb=new ProductDb(); public DropDown() { Page.Init += new System.EventHandler(Page_Init); } private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { BindProduct(); } } private void Page_Init(object sender, EventArgs e) { InitializeComponent(); } void BindProduct() { //pdb.GetProduct() returns a datatable to Product@#s datagrid ProductGrid.DataSource=pdb.GetProduct(); ProductGrid.DataBind(); } protected void Product_Edit(object sender, DataGridCommandEventArgs e) { BindCategory(); ((DataGrid)sender).EditItemIndex=e.Item.ItemIndex; BindProduct(); } protected void Product_Cancel(object sender, DataGridCommandEventArgs e) { ProductGrid.EditItemIndex=-1; BindProduct(); } protected void Product_Update(object sender, DataGridCommandEventArgs e) { //get the currnet product name string pname=e.Item.Cell[1].Controls[0].Text; //get the current product price string price=e.Item.Cell[2].Controls[0].Text; //get the current categoryid DropDownList ddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1"); string categoryid=ddl.SelectedItem.Value; //get the current productid string pid=e.Item.Cell[4].Controls[0].Text; //call pdb@#s update function pdb.update(pid,pname,price,categoryid); ProductGrid.EditItemIndex=-1; BindProduct(); } void BindCategory() { //pdb.FetchCategory() returns a datatable _category=pdb.FetchCategory(); } protected DataTable GetCategory() { return _category; } protected int GetCategoryID(string cname) { for(int i=0;i<_category.DefaultView.Count;i++) { if (_category.DefaultView[i]["categoryname"].ToString()==cname) { return i; } } return 0; } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion }}The key points of this C# file are:
1.In Product_Edit() function, you have to call BindCategory() to set the _category datatable first, and then set the EditItemIndex for the datagrid, and at last, call BindProduct() function. The DropDownList control will not display anyting if you reverse this order. Because once you set the EditItemIndex, the datagrid begings rendering records, and at the same time, the DropDownList control access the function @#GetCategory()@# to get the data source, if @#GetCategory()@# returns nothing, you will not get anything of course. Just remember: before setting EditItemIndex of datagrid, set the data source of the control.
2.In Product_Update() function, You have no access to the DropDownList control directly which is embeded in the datagrid, the solution of getting the selected value of DropDownList control is the @#FindControl()@# function. This function takes the DropDownList control@#s name as its argument, and return the DropDownList control it found, so that you can use the return control to get the selected value. Just remember: use FindControl() function to return any control you want to find in the datagrid, such as text box, text area, label, calendar.
下一篇:KW大师精品文章赏析 >>
相关文章:
- · 关于返回前面的页面。如何两者兼得,自问自答
- · 在ASP.NET中的变量数值管理------看了这个我基本上对原来的REQUEST.FORM的方法传递变量绝望了
- · XML技术上传文件-转贴
- · 在datagrid中的HyperLinkColumn上达到谈出一个窗口的效果
- · 再datagrid中使用droplist。。。。重要的是其中的几个用法
- · 一个用C#做的HTTP SERVER(从WINFORM搬来的)
- · ViewState 到底是什么?
- · 允许用户一次上传多个文件
- · 用C# 实现Web文件的上传
- · ASP.NET Caching
- · ASP.NET Caching(2)
- · ASP.NET ViewState 初探 (3) 转自msdn
- · ASP.NET ViewState 初探 (2) 转自msdn
- · ASP.NET ViewState 初探 (1) 转自msdn
- · ASP.NET Framework深度历险(3)
- · ASP.NET下根据QueryString决定使用哪块javascript的两种方法 :)
- · ASP.NET Framework深度历险(2)
- · 图片上传的功能简介及web.config设置(自动生成所略图)
- · 图片上传的数据库部分(自动生成所略图)
- · 图片上传的WebForm(自动生成所略图)
- · 图片上传的Codebehind(自动生成所略图)
- · ASP.NET中的事务处理和异常处理
- · ASP.NET中异常处理使用(详细)
- · ASP.NET Framework深度历险(1)
- · 我写的上传(upload)文件的codebehind代码(1gdt)
- · 我写的上传(upload)文件的codebehind代码
- · 先装.net后装IIS的问题
- · Coalesys.WebMenu source code(partial)(5)
- · Coalesys.WebMenu source code(partial)(6)
- · Coalesys.WebMenu source code(partial)(7)
- · Coalesys.WebMenu source code(partial)(8)
- · Coalesys.WebMenu source code(partial)(9)
- · Coalesys.WebMenu source code(partial)(4)
- · Coalesys.WebMenu source code(partial)(10)
- · Coalesys.WebMenu source code(partial)(2)
- · Coalesys.WebMenu source code(partial)(1)
- · Coalesys.WebMenu source code(partial)(3)
- · .NET Framework環境下的ASP網頁製作(1) (王国荣)
