Using DropDownList control in DataGrid
来源:互联网 发布:excel数据分析图 编辑:程序博客网 时间:2024/06/16 02:28
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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:
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Using DropDownList control in DataGridBy 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, caleng/wangzhanyouhua/" title="seo,搜索引擎优化">seover="window.status='正文--Using DropDownList control in DataGrid';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- Using DropDownList control in DataGrid
- Using DropDownList control in DataGrid
- Multiple selection using DropDownList in DataGrid
- Using the DataGrid Control in Pocket PC Applications
- Part 6 - Generating a dropdownlist control in mvc using HTML helpers
- csharp: Data binding in WPF DataGrid control
- csharp: Data binding in WPF DataGrid control
- Control WRL embedded in html using Javascript
- Using Keyboard Interface Control in Windows
- Using VS2010 Develop ActiveX Control in Windows Form Control Libarary
- Formatting a Flex DataGrid control using a custom
- Resizing a Flex DataGrid control using the rowCount property
- how to using CommandArgument in DataGrid
- DataGrid Control, EditText Control
- Implement Custom Paging in the ASP.Net Datagrid Control...
- Drag and Drop support for column reordering in DataGrid control
- using datagrid
- Using extjs calendar control (DateField) in ASP.NET
- PHP中的Java扩展
- 《.net编程先锋C#》第七章 异常处理(转)
- 如何用PHP调用自己编写的COM组件?
- 《.net编程先锋C#》第二章 理论基础-公用语言 运行环境(转)
- PHP中的模板技术
- Using DropDownList control in DataGrid
- 在PHP中操作MySQL要注意哪些问题?
- 《.net编程先锋C#》第五章 类(下)(转)
- PHP中的正则表达式
- KW大师精品文章赏析
- PHP如何调用JAVA 类库
- 实现一个客户端的DataSet-----index.htm
- 新版mysql+apache+php Linux安装指南
- 实现一个客户端的DataSet-----ClientDataSetDataProvider.asmx