ASP.NET GridView 行何时实际添加到根表?
目标:对于网格中的每一行,在其下方渲染一行,该行跨越网格中的所有列。
原因:这样我们就可以在该行中嵌套网格,或者可以是用于快速编辑的表单,也可以是更新面板。
挑战:在 RowDataBound 中,您正在使用的 RowCreated 行尚未添加到根表中。如果您想在该行之前添加一行,您可以将 e.row.parent 控件转换为表并将该行添加到其中,这样就很容易。当您这样做时,它将显示在您正在使用的当前行之前。
示例:
protected void Page_Load(object sender, EventArgs e)
{
List<String> strings = new List<string>();
strings.Add("Test1");
strings.Add("Test2");
strings.Add("Test3");
CustomGridView GridView1 = new CustomGridView();
GridView1.DataSource = strings.Select(s => new { Column1 = s });
GridView1.DataBind();
phGrid.Controls.Add(GridView1);
}
public class CustomGridView : GridView
{
public Table Table
{
get;
set;
}
public CustomGridView()
{
this.RowCreated += new GridViewRowEventHandler(CustomGridView_RowCreated);
}
protected override Table CreateChildTable()
{
Table = base.CreateChildTable();
return Table;
}
GridViewRow lastRow;
void CustomGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
{
if (lastRow != null)
this.Table.Rows.Add(CreateRow());
lastRow = e.Row;
}
}
private static GridViewRow CreateRow()
{
GridViewRow newRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.Controls.Add(new LiteralControl(" FormRow"));
newRow.Cells.Add(cell);
return newRow;
}
}
问题: 代码味道比我多一点想要这个代码。我不喜欢我需要跟踪最后一个数据行,因为初级开发人员很难理解此代码的迭代性质以进行维护。
问题:有谁知道创建行后会调用什么?该行何时实际添加到根表中?我可以重写一个方法来参与它吗?我已经用 Reflector 反编译了 GridView 类,但我只是看不到我要找的东西。
Goal: For each row in the grid render a row under it that colspans all of the columns in the grid.
Why: So that we can nest grids in that row or maybe a form for a quick edit or maybe an update panel.
Challenge: In RowDataBound, RowCreated the row that you are working with has not yet been added to the root table. This makes it easy if you want to add a row before that row you can cast the e.row.parent control as table and add the row to it. When you do that it will show up before the current row you are working with.
Example:
protected void Page_Load(object sender, EventArgs e)
{
List<String> strings = new List<string>();
strings.Add("Test1");
strings.Add("Test2");
strings.Add("Test3");
CustomGridView GridView1 = new CustomGridView();
GridView1.DataSource = strings.Select(s => new { Column1 = s });
GridView1.DataBind();
phGrid.Controls.Add(GridView1);
}
public class CustomGridView : GridView
{
public Table Table
{
get;
set;
}
public CustomGridView()
{
this.RowCreated += new GridViewRowEventHandler(CustomGridView_RowCreated);
}
protected override Table CreateChildTable()
{
Table = base.CreateChildTable();
return Table;
}
GridViewRow lastRow;
void CustomGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
{
if (lastRow != null)
this.Table.Rows.Add(CreateRow());
lastRow = e.Row;
}
}
private static GridViewRow CreateRow()
{
GridViewRow newRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.Controls.Add(new LiteralControl(" FormRow"));
newRow.Cells.Add(cell);
return newRow;
}
}
Issues: The codesmell is a little bit more than I would like with this code. I don't like that I need to keep track of the last data row because it is hard for junior developers to comprehend the iterative nature of this code for maintenance.
Question: Does anyone know what gets called just after row created? When is the row actually added to the root table and can I override a method to participate with it. I have decompiled the GridView class with Reflector and I just can't see to find what I am looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽可能快速和肮脏,但你可以考虑添加类似的内容:
在 gridview 的最后一列(itemtemplate)中
Quick and dirty as can, but you could consider adding something like:
in the last column (itemtemplate) of your gridview