我想在所有列的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>
答案 0 :(得分:1)
不确定问题,认为您可以尝试为每列创建新实例。 即如果条件改变
LinkButton lnkProductName;
if (col1 != null)
{
lnkProductName= new LinkButton();
lnkProductName.Text = "Product Name";
col1.Controls.Add(lnkProductName);
lnkProductName= new LinkButton();
lnkProductName.Text = "Product Name2";
col2.Controls.Add(lnkProductName);
}
类似于其他列
答案 1 :(得分:0)
使用此:
if (e.Row.RowIndex == 0)
{
if (col1 != null)
{
LinkButton lnkProductNameCol1 = new LinkButton();
lnkProductNameCol1.Text = "Product Name";
col1.Controls.Add(lnkProductNameCol1);
LinkButton lnkProductNameCol2 = new LinkButton();
lnkProductNameCol2.Text = "Product Name";
col2.Controls.Add(lnkProductNameCol2);
LinkButton lnkProductNameCol3 = new LinkButton();
lnkProductNameCol3.Text = "Product Name";
col3.Controls.Add(lnkProductNameCol3);
LinkButton lnkProductNameCol4 = new LinkButton();
lnkProductNameCol4.Text = "Product Name";
col4.Controls.Add(lnkProductNameCol4 );
}
}
希望得到这个帮助。