mgmhicks
asked on
Why wont datagrid update after delete
I have the following code in code behind
This is what the html page looks like for the most part
So my question is I can add a role and the grid updates and all is well. However if I go to delete the row, the grid is not displayed correctly unless on go from 1 grid page to the next, ( i set the grid page length to 3 to test) or refresh the entire page.
When I tell it to displayroles it does have in there the correct number of rows, however the grid doesnt change.
Any Ideas?
thanks
Private Sub DisplayRoles()
Try
gvRoles.DataSource = Roles.GetAllRoles()
gvRoles.DataBind()
Catch ex As Exception
Dim myError As String = ex.Message
End Try
End Sub
Private Sub gvRoles_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gvRoles.PageIndexChanging
gvRoles.PageIndex = e.NewPageIndex
DisplayRoles()
End Sub
Private Sub gvRoles_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvRoles.RowCommand
Try
If e.CommandName = "Delete" Then
Dim myRow As String = e.CommandArgument
Roles.DeleteRole(myRow)
DisplayRoles()
Else
End If
Catch ex As Exception
Dim myError As String = ex.Message
End Try
End Sub
This is what the html page looks like for the most part
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-2">
<asp:UpdatePanel ID="pnlRoles" runat="server" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvroles" EventName="RowCommand" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gvRoles" runat="server" CssClass="table table-bordered table-striped table-responsive pagination-ys" AllowPaging="True" AutoGenerateColumns="False" PageSize="3">
<Columns>
<asp:TemplateField HeaderText="Role Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove" FooterStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False"
CommandArgument="<%# Container.DataItem %>" CommandName="Delete" Text="" ImageUrl="~/Images/delete.png" ItemStyle-VerticalAlign="Top" Height="50px" Width="50px"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="table" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-2 col-sm-3">
<b>Create a New Role</b>
</div>
<div class="col-md-2 col-sm-2">
<asp:TextBox ID="txtRoleName" runat="server">
</asp:TextBox>
</div>
<div class="col-md-3">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Role is a Required Field"
ControlToValidate="txtRoleName" CssClass="text-danger" ></asp:RequiredFieldValidator>
</div>
</div>
<div class="row">
<br />
<asp:Button ID="CreateRoleButton" runat="server" Text="Create Role" CssClass="btn-success" />
</div>
</div>
So my question is I can add a role and the grid updates and all is well. However if I go to delete the row, the grid is not displayed correctly unless on go from 1 grid page to the next, ( i set the grid page length to 3 to test) or refresh the entire page.
When I tell it to displayroles it does have in there the correct number of rows, however the grid doesnt change.
Any Ideas?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Change
Private Sub gvRoles_RowCommand
to
Protected Sub gvRoles_RowCommand
Private Sub gvRoles_RowCommand
to
Protected Sub gvRoles_RowCommand
ASKER
ok, that made your code work, but I still have to refresh. When it hits the code gvroles.datasource=roles.g etallroles () I have the correct number of rows going to the grid. But the grid isn't updating. It is in an update panel, but if I add it updates right away going through the same code.
ASKER
what it ended up being is that the rowdeleting event was not handle and I didn't see that error when it was in a panel. simply added the event and it worked the way it supposed to. Not sure why is triggers rowdeleting when we are not deleting a row from the grid, but that seemed to take care of it. Any ideas?
ASKER
gvRoles_RowCommand' is not a member of 'ASP.administration_manage
seems clearly to be there