0

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

4 回答 4

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);
            }
        }
    }
}
于 2013-11-13T04:18:31.907 回答
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

    }
}  
于 2013-11-13T04:33:45.330 回答
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>
于 2013-11-13T04:18:25.970 回答
-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;
            }
        }

    }
于 2013-11-13T04:38:34.243 回答