你能看到一个基于模板列实现的例子,它演示了怎样在一列中显示数据源中两个字段。在不是基于模板的列中只能获得(显示)一个字段。这个模板的代码如下所示: <itemtemplate> <%# "<b>" + DataBinder.Eval(Container.DataItem, "lastname") + "</b>, " + DataBinder.Eval(Container.DataItem, "firstname") %> </itemtemplate> 如果你需要结合更多的字段在同一个列中显示,模板列是唯一的方法。如果你需要对某个单元格应用一种特殊的格式,你最好就用datagrid的ItemCreated 事件或者ItemDataBound事件。例如,如果你要改变某个单元格的背景色,或者你要根据某种条件来对单元格应用其它的样式表,你可以在ItemCreated的处理事件中,确定正在创建的项(item)是你需要处理的类型(item 或者AlternatingItem类型),然后应用新的或已存在的样式表。在ItemCreated事件触发时,仍不能保证那一项(item)已经绑定了数据。通过ItemCreated事件的参数e的dataitem属性可以得到触发该事件的datagriditem对象。对于datagrid的ItemCreated事件,它的事件参数是DataGridItemEventArgs类型: void ItemCreated(Object sender, DataGridItemEventArgs e) e.Item.DataItem的值在项被创建时根据数据源的不同赋予不同值。如果datagrid绑定的是一个datatable,那么DataItem就是一个DataRow对象。正如我刚才所说,虽然大多数情况下数据绑定(data binding)事件在ItemCreated触发时还没有触发。但实际上,数据绑定事件(data binding)通常发生在ItemDataBound事件触发时。这个规则只有一种情况下例外,就是在基于模板的列中。一个模板列不止能绑定数据源中的某个字段;它还能访问整个的数据源。这使我们可以在ItemCreated事件中做一些处理工作,而不必等ItemDataBound事件触发,因为itemdatabound事件在ItemCreated事件之后触发。对于bound、button以及 hyperlink列,你可能需要ItemDataBound事件来获得datagrid将要显示的数据绑定的文字。模板列的头 既然TemplateColumn类给了HeaderTemplate(FooterTemplate)属性,你就可以定制给定列的头(header)和尾(footer)。说到定制,它在这里是非常重要的一点。因为在普通的数据绑定列中没有header模板和footer模板。HeaderTemplat只能应用在TemplateColumn 类的一个实例中。对于这个实例,如果你想用一个非标准的方法编辑该列的内容(例如说你想添加对内容的验证),你可以用headertemplate,也可以用一个简单的BoundColumn类来呈现。 真的可以改变列的header布局吗?如果你需要根据某个表达式对列进行排序,排序机制是datagrid自动在列的头部插入一个hyperlink控件,当用户单击hyperlink控件时,asp.net会根据hyperlink控件的href属性触发一个回发事件,从而对该列进行排序操作。就算你不需要排序你也可以很自由的更改列的header。如果你需要用ItemCreated事件在header中增加其它的控件及脱离datagrid控件做其它的任何事情。下面请看一个例子: Figure 3 Adding the Sort Dropdown public void ItemCreated(Object sender, DataGridItemEventArgs e) { ListItemType lit = e.Item.ItemType; if (lit == ListItemType.Header) { // Create and fill a dropdown list control DropDownList dd = new DropDownList(); dd.ID = "ddSort"; ListItem li1, li2, li3; // ListItem constructor takes Text and Value for the item li1 = new ListItem("Title of courtesy", "titleofcourtesy"); dd.Items.Add(li1); li2 = new ListItem("Last Name", "lastname"); dd.Items.Add(li2); li3 = new ListItem("First Name", "firstname"); dd.Items.Add(li3); // Selects the item, if any, that was selected last time dd.SelectedIndex = Convert.ToInt32(grid.Attributes["FieldIndex"]); // Add the dropdown list to the header of the 2nd column TableCell cell = (TableCell) e.Item.Controls[1]; cell.Controls.Add(dd); } }public void SortCommand(Object sender, DataGridSortCommandEventArgs e) { // Code that retrieves the grid's data source GOES HERE ••• // Sort by the specified expression or figure it out if (e.SortExpression != "*") dv.Sort = e.SortExpression; else { // Retrieves the dropdown list control through its ID DataGridItem dgi = (DataGridItem) e.CommandSource; DropDownList dd = (DropDownList) dgi.FindControl("ddSort"); // Retrieves the sorting expression from the list dv.Sort = dd.SelectedItem.Value; // Persists the currently selected dropdown item grid.Attributes["FieldIndex"] = dd.SelectedIndex.ToString(); }
public void AddDropListTemplateColumn(int i,DataSet ds,string strHead)
{
typeCol=1;
TemplateColumn tc = new TemplateColumn();
tc.HeaderText = strHead;
tc.ItemTemplate = new DropListTemplate(ds,strHead);
grid.Columns.AddAt(i,tc);
}
DropListTemplate就是你要先建的类
{
}
在用 zsms2(SharpSun) 的方法
<ItemTemplate>
<asp:TextBox id="txtFValue1" runat="server" Text='<%# DataBinder.Eval (Container.DataItem, "FValue1") %>' Width="120px" />
</ItemTemplate>
我想在服务器端动态实现同样的功能
<itemtemplate>
<%#
"<b>" +
DataBinder.Eval(Container.DataItem, "lastname") +
"</b>, " +
DataBinder.Eval(Container.DataItem, "firstname")
%>
</itemtemplate>
如果你需要结合更多的字段在同一个列中显示,模板列是唯一的方法。如果你需要对某个单元格应用一种特殊的格式,你最好就用datagrid的ItemCreated 事件或者ItemDataBound事件。例如,如果你要改变某个单元格的背景色,或者你要根据某种条件来对单元格应用其它的样式表,你可以在ItemCreated的处理事件中,确定正在创建的项(item)是你需要处理的类型(item 或者AlternatingItem类型),然后应用新的或已存在的样式表。在ItemCreated事件触发时,仍不能保证那一项(item)已经绑定了数据。通过ItemCreated事件的参数e的dataitem属性可以得到触发该事件的datagriditem对象。对于datagrid的ItemCreated事件,它的事件参数是DataGridItemEventArgs类型:
void ItemCreated(Object sender, DataGridItemEventArgs e)
e.Item.DataItem的值在项被创建时根据数据源的不同赋予不同值。如果datagrid绑定的是一个datatable,那么DataItem就是一个DataRow对象。正如我刚才所说,虽然大多数情况下数据绑定(data binding)事件在ItemCreated触发时还没有触发。但实际上,数据绑定事件(data binding)通常发生在ItemDataBound事件触发时。这个规则只有一种情况下例外,就是在基于模板的列中。一个模板列不止能绑定数据源中的某个字段;它还能访问整个的数据源。这使我们可以在ItemCreated事件中做一些处理工作,而不必等ItemDataBound事件触发,因为itemdatabound事件在ItemCreated事件之后触发。对于bound、button以及 hyperlink列,你可能需要ItemDataBound事件来获得datagrid将要显示的数据绑定的文字。模板列的头
既然TemplateColumn类给了HeaderTemplate(FooterTemplate)属性,你就可以定制给定列的头(header)和尾(footer)。说到定制,它在这里是非常重要的一点。因为在普通的数据绑定列中没有header模板和footer模板。HeaderTemplat只能应用在TemplateColumn 类的一个实例中。对于这个实例,如果你想用一个非标准的方法编辑该列的内容(例如说你想添加对内容的验证),你可以用headertemplate,也可以用一个简单的BoundColumn类来呈现。
真的可以改变列的header布局吗?如果你需要根据某个表达式对列进行排序,排序机制是datagrid自动在列的头部插入一个hyperlink控件,当用户单击hyperlink控件时,asp.net会根据hyperlink控件的href属性触发一个回发事件,从而对该列进行排序操作。就算你不需要排序你也可以很自由的更改列的header。如果你需要用ItemCreated事件在header中增加其它的控件及脱离datagrid控件做其它的任何事情。下面请看一个例子:
Figure 3 Adding the Sort Dropdown
public void ItemCreated(Object sender, DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.Header)
{
// Create and fill a dropdown list control
DropDownList dd = new DropDownList();
dd.ID = "ddSort";
ListItem li1, li2, li3; // ListItem constructor takes Text and Value for the item
li1 = new ListItem("Title of courtesy", "titleofcourtesy");
dd.Items.Add(li1); li2 = new ListItem("Last Name", "lastname");
dd.Items.Add(li2); li3 = new ListItem("First Name", "firstname");
dd.Items.Add(li3); // Selects the item, if any, that was selected last time
dd.SelectedIndex = Convert.ToInt32(grid.Attributes["FieldIndex"]); // Add the dropdown list to the header of the 2nd column
TableCell cell = (TableCell) e.Item.Controls[1];
cell.Controls.Add(dd);
}
}public void SortCommand(Object sender, DataGridSortCommandEventArgs e)
{
// Code that retrieves the grid's data source GOES HERE
••• // Sort by the specified expression or figure it out
if (e.SortExpression != "*")
dv.Sort = e.SortExpression;
else
{
// Retrieves the dropdown list control through its ID
DataGridItem dgi = (DataGridItem) e.CommandSource;
DropDownList dd = (DropDownList) dgi.FindControl("ddSort"); // Retrieves the sorting expression from the list
dv.Sort = dd.SelectedItem.Value; // Persists the currently selected dropdown item
grid.Attributes["FieldIndex"] = dd.SelectedIndex.ToString();
}
// Refreshes the grid
grid.DataBind();
}
简单的方法是通过一个扩展的ASCX文件设置模板,ascx文件是用户控件文件,它声明了在template中的html 代码(文字)和asp.net控件。下面的代码是ascx文件的典型结构:
<%@ Language="C#" %>
<%# layout goes here %>
在ascx文件的布局中显式的声明language是非常重要的。不管你的ascx文件中是否有代码你都要声明language。你可以声明为任何.net支持的language(可以编译成MSIL的语言)即使这种语言与其所在页的语言不一样。建一个模板文件,后追是.ascx,用的时候用LoadTemplate方法导入,LoadTemplate方法从外部文件获取 ITemplate 接口的实例
演示:http://218.84.107.5/g.aspx
下载:http://218.84.107.5/g.rar