2009-06-02 76 views
2

是否有一种有效的方法来覆盖ASP.NET GridView控件的页眉和页脚的HTML输出?覆盖ASP.NET gridview控件的头部输出

我想实现一个类似于HeaderTemplate标记的方法,这个方法与ASP.NET Repeater中的标记类似,或者不需要在后面的页面代码中动态构建HTML输出。如果ASP.NET GridView控件提供这两种类型的选项。

回答

0

您可能要考虑使用ASP.Net Control Adapter进行调查。我已经使用它们很基本的东西,但正如Scott Guthrie笔记:

控制适配器可 插件到任何ASP.NET服务器控件 和重写,修改和/或调整的 渲染输出该控制的逻辑。

Toolkit还包含几个“开箱”适配器,您可以从中找到示例,包括GridView。同样,我不是100%肯定你能够做到你想要的,但值得肯定的是值得一看。如果无非就是把另一个ASP.Net技巧放在你的背后。

+0

感谢您的信息。我会考虑下一个项目。 – 2009-06-03 16:27:00

1

在GridView,您可以使用RowCreated事件彻底“摧毁”并重新创建页眉和/或页脚。在这种情况下,请检查:

if (e.Row.RowType = DataControlRowType.Header) 
{ 
    // At this point you have access to e.Row.Cells 
    // You can now empty the collection and recreate it. 
    // If you create a singular cell in the collection 
    // you can then make its ColumnSpan reach across 
    // the length of the entire table. Then inside this 
    // cell you can add any set of controls you want. 
    // I've used this method to combine column headers 
    // and add specialty controls that simply wouldn't 
    // working using the HeaderTemplate 
} 

由于这是在做RowCreated,期间的RowDataBound你将有机会获得这些控件,但是可以再操纵他们你喜欢基于数据。这对于在页脚中执行复杂的计算,基于排序等调整标题中的图像而言是方便的。

0

要在RowCreated上更改逐个单元格的检查,请执行如下操作如果你想添加一个下拉列到允许过滤的列,你可以做。

if (e.Row.RowType = DataControlRowType.Header) 
{ 
    e.Row.Cells[0].Controls.Clear(); 

    var ddlFilter = new DropDownList(); 
    //add options etc 

    e.Row.Cells[0].Controls.Add(ddlFilter); 
} 

如果你要转换为单个细胞并添加新的控件,那么我会刚刚成立ShowHeader=false,并把我的标记/控制gridview的上述