几天前,当我在开发一个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.youkuaiyun.com/TrackBack.aspx?PostId=32290