<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.
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:
新闻热点
疑难解答