Link to home
Start Free TrialLog in
Avatar of RobertNZana
RobertNZanaFlag for United States of America

asked on

Used RowCommand - now my standard 'edit' doesn't work!

I am using a databound gridview control.  I have a standard 'edit' button (with save/cancel.)  I wrote no code for this.  It worked great!

Then I added a template field for a 'delete' linkbutton which does some CUSTOM events in addition to deleting.

I use the RowCommand event of the gridview in order to run the custom 'delete' code.  But now my 'edit' button (no code) doesn't work.  Any reason why?
Protected Sub gvSubusers_RowCommand(ByVal sender As Object, ByVal e As 
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles 
gvSubusers.RowCommand
        If e.CommandName = "DeleteSubUser" Then
            Dim u As New MyCompany.BusinessLogicLayer.Users.Users
            Try
                Dim lb As LinkButton = CType(e.CommandSource, LinkButton)
                Dim row As GridViewRow = CType(lb.NamingContainer, 
GridViewRow)
                Dim id As Integer = 
CInt(gvSubusers.DataKeys(row.RowIndex).Values(0))
 
                u.DeleteSubuser(id)    
                Page.DataBind()
            Catch ex As Exception
                lblStatus.Text = ex.ToString
                lblStatus.ForeColor = Drawing.Color.Red
            End Try
        End If
    End Sub
 
'******* HERES THE GRIDVIEW ***********
 
                <asp:GridView ID="gvSubusers" runat="server" CssClass="aspnet_gridview_padleft" AllowPaging="True"
                    AllowSorting="True" AutoGenerateColumns="False" DataSourceID="odsSubUsers" 
                    SkinID="Style2" DataKeyNames="PermissionId">
                    <Columns>
                        <asp:TemplateField HeaderText="Username">
                            <ItemTemplate>
                                <asp:Label ID="lblUsername2" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblUsername" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                                <asp:Label ID="lblMyCompanyUserId" runat="server" Visible="false" Text='<%# bind("MyCompanyUserId") %>'></asp:Label>
                                <asp:Label ID="lblPermissionId" runat="server" Visible="false" Text='<%# bind("PermissionId") %>'></asp:Label>
                            </EditItemTemplate>
                            <HeaderStyle Width="100px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="First Name">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtFirstName" runat="server" Text='<%# bind("FirstName") %>' Width="90px"></asp:TextBox>
                            </EditItemTemplate>
                            <HeaderStyle Width="100px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Last Name">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtLastName" runat="server" Text='<%# bind("LastName") %>' Width="90px"></asp:TextBox>
                            </EditItemTemplate>
                            <HeaderStyle Width="100px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Email">
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEmail" runat="server" Text='<%# bind("Email") %>' Width="110px"></asp:TextBox>
                            </EditItemTemplate>
                            <HeaderStyle Width="110px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Edit">
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("AllowEditProjects") %>'
                                    Enabled="false" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:CheckBox ID="ckEdit" runat="server" Checked='<%# Bind("AllowEditProjects") %>' />
                                <asp:Label ID="lblPermissionId" runat="server" Text='<%# bind("PermissionId") %>'
                                    Visible="False"></asp:Label>
                            </EditItemTemplate>
                        </asp:TemplateField>                                                         
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="btnDelete" runat="server" CommandName="DeleteSubUser" OnClientClick="return confirm('Are you sure you want to PERMANENTLY DELETE this SubUser?');"
                                    Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        No sub-users yet.
                    </EmptyDataTemplate>
                    <EditRowStyle CssClass="" />
                </asp:GridView>

Open in new window

Avatar of techExtreme
techExtreme
Flag of India image

Hi, set your edit button's command name to "Edit".
Als for deleting stuff, you should ideally use row deleting event (see here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx)
For editing, set a control's command name to 'edit' and use row editing. Row command should only be used for other commands ideally.
Avatar of RobertNZana

ASKER

Thanks for your reply.  Actually I DID have the edit column in there, defined as below.... I just left it out of the above by accident...

Any reason why it doesn't work, now that I have code for the custom delete?  (It worked before.)
<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
            Text="Edit"></asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
            Text="Update" ValidationGroup="None"></asp:LinkButton>
        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
            Text="Cancel" ValidationGroup="None"></asp:LinkButton>
    </EditItemTemplate>
</asp:TemplateField>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of techExtreme
techExtreme
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But here's my point:  I want to use the BUILT IN edit and not write 1 line of code!  It was working before.  The 'edit' command name is in the column.  It was working before I added the custom code for the DELETE.  Any ideas?