小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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

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

動(dòng)態(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);

            動(dòng)態(tài)綁定模板列稍微復(fù)雜:


           

首先創(chuàng)建一個(gè)類,該類時(shí)繼承了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)鍵實(shí)現(xiàn)這個(gè)方法

                {

                    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í)例時(shí),為此屬性賦值(數(shù)據(jù)源字段)

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

               

            }

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


           

頁面使用時(shí)


           

2.*.aspx頁面后臺(tái)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();

            這樣就會(huì)添加了一個(gè)textbox的模板列;


           

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多