国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Using dropDownList control in DataGrid

2024-07-21 02:16:42
字體:
來源:轉載
供稿:網友
using dropdownlist control in datagrid
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:
<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, calendar. 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明溪县| 彭州市| 谢通门县| 米泉市| 琼结县| 定日县| 阿拉善左旗| 梅州市| 晋宁县| 黄浦区| 化德县| 凤台县| 林州市| 信丰县| 东丽区| 清远市| 海宁市| 辽源市| 大同市| 福州市| 玛沁县| 和林格尔县| 奇台县| 黔南| 出国| 武山县| 鄂尔多斯市| 广德县| 商城县| 来凤县| 信丰县| 舒兰市| 曲周县| 翁源县| 东宁县| 嵩明县| 巴青县| 白朗县| 九寨沟县| 张家川| 阿坝县|