5
我想创建一个扩展在GridView中使用的BoundField的控件。我想要做的是提供另一个名为HighlightField的属性,它将类似于DataField属性,我想给它一个数据列的名称。鉴于该数据列,它会查看该值是true还是false,并突出显示给定行上给定列内的给定文本。扩展(ASP.NET)BoundField
一些伪代码,如果是没有意义:
<asp:GridView id="grid">
<Columns>
<asp:BoundField DataField="Name" />
<cc:HighlightField DataField="Name" HighlightField="IsHighlighted" />
</Columns>
</asp:GridView>
再内的数据绑定或东西:
if(this row's IsHighlighted value is true)
set the CssClass of this datacell = "highlighted"
(or wrap a span tag around the text)
奸污我指出正确的方向,这是我结束了:
public class HighlightedBoundField : BoundField
{
public string HighlightField
{
get { return ViewState["HighlightField"].ToString(); }
set
{
ViewState["HighlightField"] = value;
OnFieldChanged();
}
}
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
bool isDataRowAndIsHighlightFieldSpecified = cellType == DataControlCellType.DataCell && !string.IsNullOrEmpty(HighlightField);
if (isDataRowAndIsHighlightFieldSpecified)
{
cell.DataBinding += new EventHandler(cell_DataBinding);
}
}
void cell_DataBinding(object sender, EventArgs e)
{
TableCell cell = (TableCell)sender;
object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
cell.Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString();
bool highlightThisCellsText = Convert.ToBoolean(DataBinder.GetPropertyValue(dataItem, HighlightField));
if (highlightThisCellsText)
{
cell.CssClass += " highlight";
}
}
}
我喜欢它,我会给它一个旋转和回 – rball 2009-09-04 18:03:32
有没有办法做双向数据绑定?你可以在你的HighlightBoundField上创建一个名为“IsHighlighted”的布尔属性,并执行如下操作:'/> –
Chris
2011-01-04 17:57:12