locked
Can I use custom Delete button in place CommandField? RRS feed

  • Question

  • User1216627406 posted

    The code below allows to dynamically add and remove rows, among other things it does.

    Is it possible to change the delete from CommandField to a custom delete button?

       <table>
        <tr>
            <td>
                <asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="Grid1CheckChanged" /><span style="color:#ff0000">*</span>
            </td>
            <td>
              <asp:gridview ID="Gridview1" gridlines="None" runat="server" ShowFooter="true" AutoGenerateColumns="false" onrowdatabound="Gridview1_RowDataBound" OnRowDeleting="Gridview1_RowDeleting">
                <Columns>
                <asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
                <asp:TemplateField HeaderText="Name">
                 <headerstyle horizontalalign="Left" />
                    <ItemTemplate>
                        <asp:TextBox ID="txtsourcename" placeholder="Name...(e.g, Jane Doe)" runat="server" style="width:250px;" class="form-control"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    <ItemTemplate>
                        <asp:TextBox ID="txtsourceaddress" placeholder="Address..." runat="server" style="width:250px;" class="form-control"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Income">
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    <ItemTemplate>
                         <asp:TextBox ID="txtsourceincome" placeholder="Income...(example: 1000)" runat="server" style="width:250px;" class="form-control txtsourceincome numeric"></asp:TextBox>
                    </ItemTemplate>
                           </asp:TemplateField>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                     <asp:Button ID="ButtonAdd" runat="server" Text="Add" 
                            onclick="ButtonAdd_Click" CssClass="grvAddButton" OnClientClick="return ValidateEmptyValue();return validate()" />
                    </ItemTemplate>
                </asp:TemplateField>
               <asp:CommandField ShowDeleteButton="True"><ControlStyle CssClass="grvDelButton" /></asp:CommandField>
                </Columns>
              </asp:gridview>
            </td>
        </tr>
    </table>

    Friday, December 15, 2017 8:16 PM

Answers

  • User1564875471 posted

    If I remember well, I think you can replace the command field with a template field and place a standard asp.net button but give it CommandName="Delete" attribute. This will make it a delete command.

    <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                     <asp:Button ID="ButtonAdd" runat="server" Text="Delete" CommandName="Delete"
                             CssClass="grvAddButton" OnClientClick="return confirm('are you sure?')"  />
                    </ItemTemplate>
                </asp:TemplateField>

    I think there is nothing else needed, it should automaticlaly trigger the rowDelete event

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 15, 2017 11:20 PM

All replies

  • User1564875471 posted

    If I remember well, I think you can replace the command field with a template field and place a standard asp.net button but give it CommandName="Delete" attribute. This will make it a delete command.

    <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                     <asp:Button ID="ButtonAdd" runat="server" Text="Delete" CommandName="Delete"
                             CssClass="grvAddButton" OnClientClick="return confirm('are you sure?')"  />
                    </ItemTemplate>
                </asp:TemplateField>

    I think there is nothing else needed, it should automaticlaly trigger the rowDelete event

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 15, 2017 11:20 PM
  • User1216627406 posted

    Thank you very much sir. It worked.

    Saturday, December 16, 2017 1:31 AM