日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

GridView 動態(tài)添加綁定列和模板列

 行走在理想邊緣 2019-07-03

動態(tài)添加綁定列很簡單:例如:


           

GridView1.DataSourceID = "SqlDataSource1";

           

                    BoundField bf1 = new BoundField();

                    BoundField bf2 = new BoundField();

                    BoundField bf3 = new BoundField();


           

        bf1.HeaderText = "Employee ID";

                    bf1.DataField = "EmployeeID";

                    bf1.ReadOnly = true;

                    bf1.SortExpression = "EmployeeID";

                    bf2.HeaderText = "First Name";

                    bf2.DataField = "FirstName";

                    bf2.SortExpression = "FirstName";


           

        bf3.HeaderText = "Last Name";

                    bf3.DataField = "LastName";

                    bf3.SortExpression = "LastName";


           

        CommandField cf = new CommandField();

                    cf.ButtonType = ButtonType.Button;

                    cf.ShowCancelButton = true;

                    cf.ShowEditButton = true;


           

        GridView1.Columns.Add(bf1);

                    GridView1.Columns.Add(bf2);

                    GridView1.Columns.Add(bf3);

                    GridView1.Columns.Add(cf);

            動態(tài)綁定模板列稍微復雜:


           

首先創(chuàng)建一個類,該類時繼承了System.Web.UI.ITemplate


           

public class MyTemplate:System.Web.UI.ITemplate

            {

                private string proName;

                public MyTemplate()

                {

                    //

                    //TODO: 在此處添加構(gòu)造函數(shù)邏輯

                    //

                }

                public string ProName//要綁定的數(shù)據(jù)源字段名稱

                {

                    set { proName = value; }

                    get { return proName; }

                }


           

    public void InstantiateIn(Control container)//關(guān)鍵實現(xiàn)這個方法

                {

                    TextBox hi = new TextBox();

                    hi.Text = "";

                    hi.DataBinding += new EventHandler(hi_DataBinding);//創(chuàng)建數(shù)據(jù)綁定事件

                    container.Controls.Add(hi);

                }


           

    void hi_DataBinding(object sender, EventArgs e)

                {

                    TextBox hi = (TextBox)sender;

                    GridViewRow container = (GridViewRow)hi.NamingContainer;

                    //關(guān)鍵位置

                    //使用DataBinder.Eval綁定數(shù)據(jù)

                    //ProName,MyTemplate的屬性.在創(chuàng)建MyTemplate實例時,為此屬性賦值(數(shù)據(jù)源字段)

                    hi.Attributes.Add("onclick", "alert('" + DataBinder.Eval(container.DataItem, ProName).ToString() + "');");

               

            }

            上面時創(chuàng)建了一個textbox的模板,


           

頁面使用時


           

2.*.aspx頁面后臺cs代碼

           

                        DataSet ds = null;

                        BLL.model_task bll = new BLL.model_task();

                        ds = bll.GetList(string.Empty);

           

                        TemplateField tf = new TemplateField();

                        tf.HeaderText = "自定義模板列";

                        MyTemplate mt = new MyTemplate();

                        mt.ProName = "ID";//數(shù)據(jù)源字段

                        tf.ItemTemplate = mt;

                        this.GridView1.Columns.Add(tf);

                        this.GridView1.DataSource = ds;

                        this.GridView1.DataBind();

            這樣就會添加了一個textbox的模板列;


           

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多