0

我想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>
4

2 回答 2

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);
                }

其他列类似

于 2013-03-29T07:21:06.943 回答
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 );
            }
        }

希望这有帮助。

于 2013-03-29T07:45:15.327 回答