一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Creating Templates Programmatically in the DataGrid Control

 johnlane 2007-06-07
Creating Templates Programmatically in the DataGrid Control

A DataGrid control can contain template columns in which you lay out the column by adding controls and literal text. Template columns provide you with substantially more flexibility than do bound columns, button columns, and hyperlink columns. As with templates for the DataList and Repeater controls, you can dynamically create templates for the DataGrid control. This allows you to define the contents, layout, and data of a column at run time.

Note   The procedures for working with a dynamic template column are similar to working with a dynamic template in a Repeater or DataList control. For general information on creating dynamic templates, see Creating Web Server Control Templates Dynamically.

The following are the differences between using a dynamic template in a DataGrid control and in the Repeater or DataList controls:

  • You do not create item templates for the grid itself; instead, you create them for a column in the grid.
  • There are slightly different templates for a DataGrid column than for a Repeater or DataList control. A DataGrid column does not include an alternating item or separator template. However, like the DataList control, it does include an EditItem template.

To create a dynamic template column

  1. Create a template class that implements the ITemplate interface of the System.Web.UI namespace. For details, see Creating Web Server Control Templates Dynamically.
  2. Optionally, pass into the class‘s constructor a value that the class can use to determine what type of template to create (ItemTemplate, EditItemTemplate, and so on).

    Typically, the template type is passed using a value from the ListItemType enumeration already defined in the DataGrid control.

  3. In the class, implement the InstantiateIn method (the only member of the ITemplate interface). This method provides a way to insert an instance of text and controls into the specified container.

    The following example shows a template class for a dynamic template column. The constructor accepts two parameters: the first parameter specifies the template type to create and the second parameter allows you to pass in the name of the column you are creating. Because this is a template for a DataGrid control, the class includes code to create an EditItem template containing a Textbox control.

    ‘ Visual Basic
        Private Class DataGridTemplate
        Implements ITemplate
        Dim templateType As ListItemType
        Dim columnName As String
        Sub New(ByVal type As ListItemType, ByVal ColName As String)
        templateType = type
        columnName = ColName
        End Sub
        Sub InstantiateIn(ByVal container As Control) _
        Implements ITemplate.InstantiateIn
        Dim lc As New Literal()
        Select Case templateType
        Case ListItemType.Header
        lc.Text = "<B>" & columnName & "</B>"
        container.Controls.Add(lc)
        Case ListItemType.Item
        lc.Text = "Item " & columnName
        container.Controls.Add(lc)
        Case ListItemType.EditItem
        Dim tb As New TextBox()
        tb.Text = ""
        container.Controls.Add(tb)
        Case ListItemType.Footer
        lc.Text = "<I>Footer</I>"
        container.Controls.Add(lc)
        End Select
        End Sub
        End Class
        // C#
        public class DataGridTemplate : ITemplate
        {
        ListItemType templateType;
        string columnName;
        public DataGridTemplate(ListItemType type, string colname)
        {
        templateType = type;
        columnName = colname;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
        Literal lc = new Literal();
        switch(templateType)
        {
        case ListItemType.Header:
        lc.Text = "<B>" + columnName + "</B>";
        container.Controls.Add(lc);
        break;
        case ListItemType.Item:
        lc.Text = "Item " + columnName;
        container.Controls.Add(lc);
        break;
        case ListItemType.EditItem:
        TextBox tb = new TextBox();
        tb.Text = "";
        container.Controls.Add(tb);
        break;
        case ListItemType.Footer:
        lc.Text = "<I>" + columnName + "</I>";
        container.Controls.Add(lc);
        break;
        }
        }
        }

After you have created the class for a dynamic template column, you can use it to assign columns to the DataGrid control at run time.

To use dynamic template columns

  1. Create an instance of the TemplateColumn class.
  2. Create an instance of your dynamic template, passing it an item type value if appropriate.
  3. Assign the instance to one of the template properties of the TemplateColumn object you created in Step 1, such as ItemTemplate, EditItemTemplate, HeaderTemplate, and so on.

    The following example shows how to use the dynamic template column to add two columns to the DataGrid control. In this example, the templates are instantiated during the page load and before the control is bound to its data source.

    ‘ Visual Basic
        Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tc1 As New TemplateColumn()
        tc1.HeaderTemplate = New _
        DataGridTemplate(ListItemType.Header, "Column1")
        tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, _
        "Column1")
        tc1.EditItemTemplate = New _
        DataGridTemplate(ListItemType.EditItem, "Column1")
        tc1.FooterTemplate = New _
        DataGridTemplate(ListItemType.Footer, "Column1")
        DataGrid1.Columns.Add(tc1)
        Dim tc2 As New TemplateColumn()
        tc2.HeaderTemplate = New _
        DataGridTemplate(ListItemType.Header, "Column2")
        tc2.ItemTemplate = New _
        DataGridTemplate(ListItemType.Item, "Column2")
        tc2.EditItemTemplate = New _
        DataGridTemplate(ListItemType.EditItem, "Column2")
        tc2.FooterTemplate = New _
        DataGridTemplate(ListItemType.Footer, "Column2")
        DataGrid1.Columns.Add(tc2)
        SqlDataAdapter1.Fill(DsCategories1)
        DataGrid1.DataBind()
        End Sub
        // C#
        private void Page_Load(object sender, System.EventArgs e)
        {
        TemplateColumn tc1 = new TemplateColumn();
        tc1.HeaderTemplate = new
        DataGridTemplate(ListItemType.Header, "Column1");
        tc1.ItemTemplate = new
        DataGridTemplate(ListItemType.Item, "Column1");
        tc1.EditItemTemplate = new
        DataGridTemplate(ListItemType.EditItem, "Column1");
        tc1.FooterTemplate = new
        DataGridTemplate(ListItemType.Footer, "Column1");
        DataGrid1.Columns.Add(tc1);
        TemplateColumn tc2 = new TemplateColumn();
        tc2.ItemTemplate = new
        DataGridTemplate(ListItemType.Item, "Column2");
        tc2.HeaderTemplate = new
        DataGridTemplate(ListItemType.Header, "Column2");
        tc2.EditItemTemplate = new
        DataGridTemplate(ListItemType.EditItem, "Column2");
        tc2.FooterTemplate = new
        DataGridTemplate(ListItemType.Footer, "Column2");
        DataGrid1.Columns.Add(tc2);
        sqlDataAdapter1.Fill(dsCategories1);
        DataGrid1.DataBind();
        }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(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)論公約

    類似文章 更多

    久久成人国产欧美精品一区二区| 欧美日韩国产黑人一区| 国产精品香蕉在线的人| 国产又粗又长又大高潮视频| 91久久精品中文内射| 日本妇女高清一区二区三区| 欧美国产日本高清在线| 中文字幕在线五月婷婷| 国产欧美日韩在线精品一二区| 国产丝袜美女诱惑一区二区| 国产一区二区三区草莓av| 亚洲女同一区二区另类| 久久精品国产99精品亚洲| 欧美成人久久久免费播放| 久热人妻中文字幕一区二区| 成人精品一区二区三区在线| 欧美一区二区三区高潮菊竹| 99秋霞在线观看视频| 亚洲精品福利入口在线| 久久精品国产亚洲av久按摩| 亚洲一区二区精品久久av| 国产户外勾引精品露出一区| 国产一区二区不卡在线播放| 日本少妇三级三级三级| 日韩国产亚洲欧美激情| 成人免费观看视频免费| 丁香六月婷婷基地伊人| 亚洲伦片免费偷拍一区| 国产精品推荐在线一区| 国产在线观看不卡一区二区| 午夜激情视频一区二区| 亚洲综合一区二区三区在线| 色哟哟哟在线观看视频| 91精品国自产拍老熟女露脸| 国产精品十八禁亚洲黄污免费观看| 一区二区三区亚洲天堂| 四季精品人妻av一区二区三区| 亚洲成人免费天堂诱惑| 午夜福利网午夜福利网| 国产传媒精品视频一区| 午夜传媒视频免费在线观看|