asp.netgridviewaspbutton

Using asp:Button with OnRowDeleting event


I'm trying to connect an asp:Button with my OnRowDeleting event in a gridview, but I don't don't know how to do that :X. I'm currently using CommandField but for my own purposes I need it with a Button instead. Can anyone help me please?

EDIT:

Let me re-explain, I have a row with Delete and Edit buttons in the same CommandField. What I'm trying to do is to hide the 'Delete' button in some specific cases for specific rows only and not necessarily for every row. That's why I'm struggling with a CommandField because there's no ID coming with it so I can't refer to it in my codebehind. But - asp:Button has an ID but I can't manage to link it with the UserAccounts_RowDeleting function. And that's my question - how to link it? :)

EDIT2:

These are part of my codes:

<asp:GridView ID="UserAccounts" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#3AC0F2" 
HeaderStyle-ForeColor="White" AutoGenerateDeleteButton="false" 
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AutoGenerateEditButton="false" 
onrowcancelingedit="UserAccounts_RowCancelingEdit" RowStyle-ForeColor="#3A3A3A" OnRowEditing="UserAccounts_RowEditing" 
PageSize="10" AllowPaging="false" onrowdeleting="UserAccounts_RowDeleting" 
OnRowUpdating="UserAccounts_RowUpdating" OnRowDataBound="UserAccounts_RowDataBound" onrowcreated="UserAccounts_RowCreated">
<Columns>
 <asp:CommandField ShowDeleteButton="true" ShowEditButton='true' ButtonType="Link" />
 <asp:BoundField  DataField="UserName" HeaderText="Username" ReadOnly="true"/>
 <asp:BoundField DataField="Email" HeaderText="Email" />
 <asp:CheckBoxField DataField="IsApproved" HeaderText="Approved?" ReadOnly="false"/>
 <asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked Out?" ReadOnly="true"/>
 <asp:CheckBoxField DataField="IsOnline" HeaderText="Online?" ReadOnly="true"/>
 <asp:BoundField DataField="Comment" HeaderText="Comment" NullDisplayText="N/A"/>
 <asp:TemplateField HeaderText="Select" >
    <ItemTemplate> 
        <asp:CheckBox ID="CheckBox1" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 
 </Columns>

//Codebehind c#
protected void UserAccounts_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Funcs.GetAdminLevel() >= 999)
        {
            username = UserAccounts.Rows[e.RowIndex].Cells[1].Text;
            if (username.ToLower().Equals(HttpContext.Current.User.Identity.Name.ToLower()))
                ActionStatus.Text = string.Format("ERROR: You can't delete your own account ('<font color=#000000><b>{0}</b></font>')!", username);
            else if (Funcs.GetAdminLevel(username) >= 1)
                ActionStatus.Text = string.Format("ERROR: You can't delete administrators' accounts ('<font color=#000000><b>{0}</b></font>')!", username);
            else
            {
                Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));
                Membership.DeleteUser(username);
                ActionStatus.Text = string.Format("User '<font color=#000000><b>{0}</b></font>' has been successfully deleted!", username);
                BindUserAccounts();
            }
        }
        else
            ActionStatus.Text = "You are not authorized to delete user accounts!";
    }

I want to change the CommandField to ItemTemplate so I can enable/disable it through the codebehind for specific rows only. Also, I want the ItemTemplate, which would be asp:Button, to use the UserAccounts_RowDeleting event and that's where my problem is, I just don't know how to link the button to the event..


Solution

  • It sounds like you pretty much have the solution already. Use a TemplateField for your button. The key part is to set the CommandName of your button to "Delete". This tells the GridView that it is a delete button and the RowDeleting event should handle it.

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
        </ItemTemplate>
    </asp:TemplateField>