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

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

再datagrid中使用droplist。。。。重要的是其中的幾個(gè)用法

2024-07-21 02:17:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在這里重要的不是要說(shuō)明droplist再datagrid中的使用,而是主要看其中語(yǔ)句的幾個(gè)特殊的用法,我會(huì)有注明的
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;//**看見(jiàn)sender的用法了嗎?很多人都很奇怪這個(gè)得用處,這就是一個(gè)例子
            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.

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 喀什市| 平安县| 天全县| 澄城县| 澄迈县| 宁津县| 唐河县| 马边| 深泽县| 酒泉市| 巍山| 龙门县| 壶关县| 威海市| 襄城县| 绩溪县| 霍林郭勒市| 大足县| 日照市| 贵州省| 巴塘县| 荥阳市| 武安市| 仙居县| 龙山县| 汨罗市| 洞口县| 诏安县| 正阳县| 黄浦区| 阳信县| 宣恩县| 安丘市| 林西县| 河间市| 乳山市| 望江县| 玉山县| 乳山市| 元氏县| 凤冈县|