|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
SQLServer是基于服务器端的中型的数据库,可以适合大容量数据的应用,在功能上管理上也要比Access要强得多。在处理海量数据的效率,后台开发的灵活性,可扩展性等方面强大。datagrid|控件?
UsingDropDownListcontrolinDataGrid
ByEricZheng
WhenIwasdevelopingawebapplicationcoupledaysago,Ifoundsomeinterestingthingsaboutthedatagrid,Iwanttosharethemwithothervs.netprogrammers,soIwrotethisarticle.ThisarticlewilldemonstratehowtouseDropDownListcontrolindatagrid.
TheessentialpartoftheDropDown.aspxfileisthefollowing:
Insecondline,wesetthedatasourceofthedropdownlistcontroltoafunctionGetCategory(),thisfunctionfetchestheCategoryrecordsfromdatabaseandreturnsadatatable.Inthelastline,wesettheSelectedIndextoafuncitonGetCategoryID,thisfunctiontakesthecurrentCategorynameasitsargument,andreturnsthelocaiton(aninteger)oftheCategoryname,thisenablestheDorpDownListcontroltodisplaythecorrectCategorynameforthecurrentrecord.
ThefollowingistheC#code:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.OleDb;
usingSystem.Configuration;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceManagement
{
publicclassDropDown:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.DataGridProductGrid;
protectedDataTable_category;
//newadatabaseclasstogetrecords,ProductDbisapublicclass
//containingseveralfunctions
protectedProductDbpdb=newProductDb();
publicDropDown()
{
Page.Init+=newSystem.EventHandler(Page_Init);
}
privatevoidPage_Load(objectsender,System.EventArgse)
{
if(!IsPostBack)
{
BindProduct();
}
}
privatevoidPage_Init(objectsender,EventArgse)
{
InitializeComponent();
}
voidBindProduct()
{
//pdb.GetProduct()returnsadatatabletoProductsdatagrid
ProductGrid.DataSource=pdb.GetProduct();
ProductGrid.DataBind();
}
protectedvoidProduct_Edit(objectsender,DataGridCommandEventArgse)
{
BindCategory();
((DataGrid)sender).EditItemIndex=e.Item.ItemIndex;
BindProduct();
}
protectedvoidProduct_Cancel(objectsender,DataGridCommandEventArgse)
{
ProductGrid.EditItemIndex=-1;
BindProduct();
}
protectedvoidProduct_Update(objectsender,DataGridCommandEventArgse)
{
//getthecurrnetproductname
stringpname=e.Item.Cell[1].Controls[0].Text;
//getthecurrentproductprice
stringprice=e.Item.Cell[2].Controls[0].Text;
//getthecurrentcategoryid
DropDownListddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
stringcategoryid=ddl.SelectedItem.Value;
//getthecurrentproductid
stringpid=e.Item.Cell[4].Controls[0].Text;
//callpdbsupdatefunction
pdb.update(pid,pname,price,categoryid);
ProductGrid.EditItemIndex=-1;
BindProduct();
}
voidBindCategory()
{
//pdb.FetchCategory()returnsadatatable
_category=pdb.FetchCategory();
}
protectedDataTableGetCategory()
{
return_category;
}
protectedintGetCategoryID(stringcname)
{
for(inti=0;i<_category.DefaultView.Count;i++)
{
if(_category.DefaultView[i]["categoryname"].ToString()==cname)
{
returni;
}
}
return0;
}
#regionWebFormDesignergeneratedcode
///
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}
ThekeypointsofthisC#fileare:
1.InProduct_Edit()function,youhavetocallBindCategory()tosetthe_categorydatatablefirst,andthensettheEditItemIndexforthedatagrid,andatlast,callBindProduct()function.TheDropDownListcontrolwillnotdisplayanytingifyoureversethisorder.Beca</p>ASP是依赖组件的,能访问数据库的组件好多就有好多种,再有就是你微软的工具可是什么都要收钱的啊! |
|