如何在gridview单元格中动态添加按钮

时间:2013-11-13 04:12:24

标签: asp.net

gridview无法在gridview中添加“批准”按钮,有人可以帮助我,谢谢

http://i260.photobucket.com/albums/ii8/elvenchan2/rowAdd.png

代码

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {         
            foreach (GridViewRow dr in GridView2.Rows)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Button btn = new Button();
                    btn.Text = "Approve";
                    btn.ID = "Approve";
                    btn.Click += new EventHandler(Approve_Click);

                    if (dr.Cells[1].Text == "1")
                    {
                        dr.Cells[10].Controls.Add((Control)btn);
                    }
                }
            }

    }

4 个答案:

答案 0 :(得分:1)

你必须先添加第10个单元格

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{         
    foreach (GridViewRow dr in GridView2.Rows)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btn = new Button();
            btn.Text = "Approve";
            btn.ID = "Approve";
            btn.Click += new EventHandler(Approve_Click);

            if (dr.Cells[1].Text == "1")
            {
                //Declare the bound field and allocate memory for the bound field.
                ButtonField btnRent = new ButtonField();
                //Initalize the DataField value.
                btn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                btn.CommandName = "Button";
                btn.ButtonType = ButtonType.Button;
                btn.Text = "Rent";
                btn.Visible = true;

                //Add the newly created bound field to the GridView.
                GridView2.Columns.Add(btn);
            }
        }
    }
}

答案 1 :(得分:0)

你可以尝试一下:

 if (e.Row.RowType == DataControlRowType.DataRow)
    {

        if (e.Row.RowState != DataControlRowState.Edit)
        {
            PlaceHolder ph = (PlaceHolder)e.Row.FindControl("PlaceHolder1");

            Button b1 = new Button();
            b1.ID = "Approve"; 
            b1.Text = "Approve"

            ph.Controls.Add(b1);
        }
    }

当然,您应该在html模板中添加占位符。像

 <ItemTemplate>            
      <asp:PlaceHolder ID="PlaceHolder1" runat="server">
          Button add here..
      </asp:PlaceHolder>   
 </ItemTemplate>

答案 2 :(得分:0)

由于没有创建按钮的条件,为什么不在TemplateField标记中添加GridView并为点击处理程序或CommandName添加按钮,如下所示:

<asp:GridView id="GridView1" runat="server" 
              OnRowCommand="GridView1_RowCommand">
    <Columns>
        ...
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button id="ButtonApprove" runat="server" 
                            CommandName="approve" Text="Approve" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

现在在您的代码隐藏中,您可以处理每个单独的批准按钮单击,如下所示:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "approve")
    {
        // Do approval logic here

    }
}  

答案 3 :(得分:-1)

Design   
 <asp:GridView ID="viewThemeTypeAssociationsGridView" runat="server" AutoGenerateColumns="False"
            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" CellSpacing="2" 
            OnRowDataBound="viewThemeTypeAssociationsGridView_RowDataBound" 
            DataSourceID="Sql_New" >
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" 
                    InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                <asp:TemplateField HeaderText ="New Column" >
                <ItemTemplate >
                <asp:Button ID="btnnew" runat ="server" Visible ="false" ></asp:Button>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
        <asp:SqlDataSource ID="Sql_New" runat="server" 
            ConnectionString="<%$ ConnectionStrings:EmployeeConnectionString %>" 
            SelectCommand="SELECT * FROM [tblnew]"></asp:SqlDataSource>


Code
protected void viewThemeTypeAssociationsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[2].Text == "&nbsp;")
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button btnnew = (Button)e.Row.FindControl("btnnew");
                btnnew.Visible = true;
            }
        }

    }