国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 勐海县| 怀柔区| 翁源县| 威信县| 河津市| 洛隆县| 平遥县| 汉川市| 织金县| 灌南县| 芮城县| 麦盖提县| 太仆寺旗| 巨鹿县| 武清区| 天津市| 景东| 达孜县| 阿拉善左旗| 来安县| 平乐县| 错那县| 常宁市| 墨江| 嘉鱼县| 革吉县| 泽普县| 稷山县| 侯马市| 南平市| 福鼎市| 金阳县| 永和县| 台北市| 历史| 洛南县| 平阳县| 临泉县| 田林县| SHOW| 竹山县|