我想gridview's
在所有列的第一行添加一个简单的链接按钮。我写了关于rowdatabound
事件的代码
我的代码如下
protected void grdCompareProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PlaceHolder col1 = e.Row.FindControl("col1") as PlaceHolder;
PlaceHolder col2 = e.Row.FindControl("col2") as PlaceHolder;
PlaceHolder col3 = e.Row.FindControl("col3") as PlaceHolder;
PlaceHolder col4 = e.Row.FindControl("col4") as PlaceHolder;
LinkButton lnkProductName = new LinkButton();
lnkProductName.Text = "Product Name";
if (e.Row.RowIndex == 0)
{
if (col1 != null)
{
col1.Controls.Add(lnkProductName);
col2.Controls.Add(lnkProductName);
col3.Controls.Add(lnkProductName);
col4.Controls.Add(lnkProductName);
}
}
}
}
但它只在最后一列显示链接按钮。网格视图的 HTML 如下..
<asp:GridView ID="grdCompareProducts" runat="server" AutoGenerateColumns="false"
ShowHeader="false" CssClass="compare-info" Width="100%" CellPadding="6" CellSpacing="0"
OnRowCommand="grdCompareProducts_RowCommand" OnRowDataBound="grdCompareProducts_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblHeader" runat="server" Text='<%#Eval("header") %>' CssClass="header_strong"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="col1" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="col2" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="col3" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="col4" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>