首页 > 开发 > 综合 > 正文

再datagrid中使用droplist。。。。重要的是其中的几个用法

2024-07-21 02:17:00
字体:
来源:转载
供稿:网友
在这里重要的不是要说明droplist再datagrid中的使用,而是主要看其中语句的几个特殊的用法,我会有注明的
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
//相信productb是作者自定义的类
        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;//**看见sender的用法了吗?很多人都很奇怪这个得用处,这就是一个例子
            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.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表