是否有一种有效的方法来覆盖ASP.NET GridView
控件的页眉和页脚的HTML输出?
我想实现一种类似于ASP.NET HeaderTemplate
中的Repeater
标记的方法,或者不需要在后面的页面代码中包含动态构建HTML输出。如果这两种类型的选项可用于ASP.NET GridView
控件。
答案 0 :(得分: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期间您可以访问这些控件,然后可以根据数据操作它们。这对于在页脚中进行复杂计算,根据排序等调整页眉中的图像非常方便
答案 1 :(得分:0)
您可能需要考虑使用ASP.Net Control Adapter。我已将它们用于非常基本的事情,但正如Scott Guthrie注意到:
控制适配器允许您 插入任何ASP.NET服务器控件 并覆盖,修改和/或调整 渲染输出逻辑 控制。
Toolkit还包含几个“开箱即用”的适配器,您可以从中获取示例,包括GridView。同样,我并不是100%确定你能够完全按照自己的意愿行事,但值得一试。如果只是把另一个ASP.Net技巧放在你的腰带上。
答案 2 :(得分:0)
您还可以继承控件并覆盖渲染功能。我必须这样做才能解决ASP.NET单选按钮的缺点。基本思路就在这里,您可以根据自己的需要进行修改:
http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx
答案 3 :(得分: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上