在GridView的单个列上添加不同的按钮

时间:2016-06-20 19:51:13

标签: c# asp.net gridview

我已使用Action向我Gridview的{​​{1}}列添加了修改按钮。我希望每列都在第一列中有编辑按钮,但最后一行除外,它应该有一个Add按钮。如何使用不同的按钮实现这个单独的最后一行?这是我的代码。

TemplateField


<asp:TemplateField HeaderText="Action">
      <ItemTemplate>
            <asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
      </ItemTemplate>
</asp:TemplateField>

3 个答案:

答案 0 :(得分:0)

该项目模板列将向每行添加其内容。解决此问题的一种方法是添加并清空列,并在OnRowDataBound事件期间添加按钮。

您可以检查您是否在最后一行,如果是,请添加另一个按钮,而不是编辑按钮。

答案 1 :(得分:0)

首先你需要添加2个按钮1st Edit(可见)和第2个Add(使它显示=最初没有)

<asp:TemplateField HeaderText="Action">
      <ItemTemplate>
            <asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
  <asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="AddClick()" />
      </ItemTemplate>
</asp:TemplateField>

然后查找Gridview最后一行并找到按钮控件

GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];
Button Edit = ((Button)row.FindControl("btnedit")).Text;
Button Add = ((Button)row.FindControl("btnadd")).Text;
Edit.visible =false;
Add.Visible =true; 

此处反转按钮显示选项:False for Edit and true for Add

答案 2 :(得分:0)

一种方法是使用页脚作为GridView的最后一行。它看起来像这样:

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" ... >
    <asp:TemplateField HeaderText="Action">
        <ItemTemplate>
            <asp:Button ID="btnEdit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
        </ItemTemplate>
        <FooterTemplate>
            <asp:Button ID="btnAdd" runat="server" text="Add" OnClientClick="btnAddClick()" />
        </FooterTemplate>
    </asp:TemplateField>    
    ....
</asp:GridView>

您可以查看本文以获取完整示例,其中包含输入控件和验证:Inserting a New Record from the GridView's Footer