1

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..

Aradmey
  • 377
  • 1
  • 3
  • 11

1 Answers1

1

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>
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • Thank you, delete button works now :) Problem now is that the edit button doesn't work. When I click edit, it lets me edit the row and everything, but it won't set the edit button to a update button like in CommandField so I can't save the edits.. – Aradmey Oct 21 '14 at 14:05
  • Great! If you made the edit button a template field as well, you'll have to manually address that issue. Do some research, give it a try and if you have another question, please create another question here on StackOverflow. [This question](http://stackoverflow.com/questions/18137137/update-cancel-buttons-dont-appear-in-templatefield-edit-button) may be a good start for you. – j.f. Oct 21 '14 at 14:14
  • Thank you very much!! The question you referred me to solved my issue. You're the best! – Aradmey Oct 21 '14 at 14:35