【问题标题】:add control in first row of each column in grid view在网格视图中每一列的第一行添加控件
【发布时间】:2013-03-29 07:05:21
【问题描述】:

我想在所有列的第一行 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>

【问题讨论】:

    标签: asp.net gridview


    【解决方案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);
                    }
    

    其他栏目类似

    【讨论】:

      【解决方案2】:

      使用这个:

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

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多