在DataGrid中使用DropDownList控件 (转 土人制造)
来源:互联网 发布:雅痞大叔范 淘宝达人 编辑:程序博客网 时间:2024/06/11 21:04
几天前,当我在开发一个Web应用程序时,我发现了关于DataGrid控件的一些有趣的事情,我想与其他VS.Net程序员共享,因此我写这篇文章,这篇文章演示如何在DataGrid中使用DropDownList控件。
下面是DropDown.aspx文件的主要部分:
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategory() %>"
DataTextField="categoryname"
DataValueField="categoryid"
SelectedIndex='<%# GetCategoryID((string)DataBinder.Eval(Container.DataItem, "categoryname")) %>'
/>
在第二行,我们设置DropDownList控件的数据源为方法GetCatgory(),该方法从数据库中获取Category记录,返回类型为DataTable。在最后一行,我们设置SelectedIndex为方法GetCategoryID(),该函数以当前的CategoryName作为参数,返回CategoryName的位置(整数),使DropDownList显示当前记录的正确CategoryName.
下面是C#代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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;
// 实例化对象
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()
{
//返回数据表,并绑定到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)
{
//产品名称
string pname=e.Item.Cell[1].Controls[0].Text;
//产品价格
string price=e.Item.Cell[2].Controls[0].Text;
//categoryid
DropDownList ddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
string categoryid=ddl.SelectedItem.Value;
//productid
string pid=e.Item.Cell[4].Controls[0].Text;
//调用update方法更新数据
pdb.update(pid,pname,price,categoryid);
ProductGrid.EditItemIndex=-1;
BindProduct();
}
void BindCategory()
{
//反回数据表
_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
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
C#文件的关键点
1.在Product_Edit()方法中,你先得调用BindCategory()方法设置_category数据表,然后设置DataGrid的EditItemIndex,最后,调用BindProduct()方法。如果你不编辑订单,DropDownList在任何情况下都不显示,因为一旦你设置了EditItemIndex,DataGrid开始提取记录,同时,DropDownList控件访问GetCategory()方法取得数据源,如果GetCategory()返回为空,将什么也得不到。
记住:设置DataGrid的EditItemIndex前,先设置控件数据源。
2.在Product_Update()方法中,没有直接访问嵌入DatGrid中的DropDownList控件,取得控件的值是通过FindControl()方法。该方法使用DropDownList控件的名称作为参数,返回查找到的DropDownList控件,以便能使用控件返回选择的值。
记住:使用FindControl()方法返回任何你想要在DataGrid中的任何控件,例如:TextBox、Label、Calendar 等。
下面是DropDown.aspx文件的主要部分:
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategory() %>"
DataTextField="categoryname"
DataValueField="categoryid"
SelectedIndex='<%# GetCategoryID((string)DataBinder.Eval(Container.DataItem, "categoryname")) %>'
/>
在第二行,我们设置DropDownList控件的数据源为方法GetCatgory(),该方法从数据库中获取Category记录,返回类型为DataTable。在最后一行,我们设置SelectedIndex为方法GetCategoryID(),该函数以当前的CategoryName作为参数,返回CategoryName的位置(整数),使DropDownList显示当前记录的正确CategoryName.
下面是C#代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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;
// 实例化对象
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()
{
//返回数据表,并绑定到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)
{
//产品名称
string pname=e.Item.Cell[1].Controls[0].Text;
//产品价格
string price=e.Item.Cell[2].Controls[0].Text;
//categoryid
DropDownList ddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
string categoryid=ddl.SelectedItem.Value;
//productid
string pid=e.Item.Cell[4].Controls[0].Text;
//调用update方法更新数据
pdb.update(pid,pname,price,categoryid);
ProductGrid.EditItemIndex=-1;
BindProduct();
}
void BindCategory()
{
//反回数据表
_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
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
C#文件的关键点
1.在Product_Edit()方法中,你先得调用BindCategory()方法设置_category数据表,然后设置DataGrid的EditItemIndex,最后,调用BindProduct()方法。如果你不编辑订单,DropDownList在任何情况下都不显示,因为一旦你设置了EditItemIndex,DataGrid开始提取记录,同时,DropDownList控件访问GetCategory()方法取得数据源,如果GetCategory()返回为空,将什么也得不到。
记住:设置DataGrid的EditItemIndex前,先设置控件数据源。
2.在Product_Update()方法中,没有直接访问嵌入DatGrid中的DropDownList控件,取得控件的值是通过FindControl()方法。该方法使用DropDownList控件的名称作为参数,返回查找到的DropDownList控件,以便能使用控件返回选择的值。
记住:使用FindControl()方法返回任何你想要在DataGrid中的任何控件,例如:TextBox、Label、Calendar 等。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=32290
- 在DataGrid中使用DropDownList控件 (转 土人制造)
- 在datagrid中添加dropdownlist控件
- 如何实现在DataGrid中实现DropDownList控件的联动
- 在formview控件模板列中使用两个DropDownList控件进行联动时的操作方法
- 在 System.Windows.Forms.DataGrid中切入使用ComboBox控件
- 在DataGrid中使用下拉列表ComboBox控件
- 在Pocket PC应用程序中使用DataGrid控件
- 在Pocket PC应用程序中使用DataGrid控件
- 在可编辑datagrid中,使用my97日期控件
- 在Pocket PC应用程序中使用DataGrid控件
- Asp.NET中DropDownList控件使用
- 使用AjaxPro实现DataGrid中DropDownList的动态绑定
- vs中使用DataGrid控件
- 在一个DataGrid中加入一个DropDownList后如何给DropDownList赋值
- ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList
- ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList
- DataGrid的使用:(一)、在DataGrid控件中实现基本的操作(编辑、删除、分页)
- DataGrid的使用:(一)在DataGrid控件中实现基本的操作(编辑、删除、分页)
- 记生平写的第一个病毒!
- 构架师已死
- 实现上传下载(jspSmartUpload)
- [亏了] base 标签解决showModalDialog 弹出新窗口问题!
- 关于unix高级环境编程(apue)编译时的err_sys和err_quit错误的解决办法
- 在DataGrid中使用DropDownList控件 (转 土人制造)
- 程序员必知必会之blog篇
- 关于ClickOnce无法更新/Upgrade/下载 应用系统最新版本的问题和解决方法
- One Night In ShangHai ---- 某转和某猫在上海的一天
- Oracle常见错误总结
- C++学习笔记(三)
- CascadingDropDown控件
- 家有宝贝(淡月儿)
- 在JSP中使用CELL插件