0

I'm having a hard time trying to setup the new aspnet identity using webforms.

Currently I'm trying to setup an admin page in order to modify a roles after they've been created.

So far I have this in my aspx page. It's the gridview that displays the roles.

<asp:GridView ID="gvRoles" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="gvRoles_RowCommand" 
                    OnRowEditing="gvRoles_RowEditing" OnPageIndexChanging="gvRoles_PageIndexChanging" OnRowCancelingEdit="gvRoles_RowCancelingEdit" 
                    OnRowDeleting="gvRoles_RowDeleting" OnRowUpdating="gvRoles_RowUpdating" AutoGenerateColumns="false" >
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <asp:CommandField ShowEditButton="True"  />
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:TemplateField HeaderText="Id">
                            <ItemTemplate>
                                <asp:Label ID="lblID" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblIDEdit" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="tbName" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

And in the gvRoles_RowUpdating event I have the following code.

protected void gvRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        context.Entry(role).State = System.Data.Entity.EntityState.Modified;
        context.SaveChanges();

        gvRoles.EditIndex = -1;

        LoadData();
    }

but for the life of me I can't figure out how to update role with the changed data in context.Entry(role).State.

Any insight would be appreciated.

Prescient
  • 1,051
  • 3
  • 17
  • 42
  • Roles would be edited only by people in the administrator role. I need to be able to do this as well as assigning people to different roles. This is my next step. – Prescient Feb 20 '15 at 20:44
  • Understood. But it's a requirement. Can't be helped. – Prescient Feb 20 '15 at 20:55
  • 2
    There's a [bolt on tool](https://github.com/IdentityManager/Thinktecture.IdentityManager) that can do that for you if you don't want to roll your own. – mason Feb 20 '15 at 21:06

1 Answers1

0

I figured it out finally.

protected void gvRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // get the row values
        Label Id = (Label)gvRoles.Rows[e.RowIndex].FindControl("lblIDEdit");
        TextBox Name = (TextBox)gvRoles.Rows[e.RowIndex].FindControl("tbName");
        // get the role from the db
        var thisRole = context.Roles.Where(r => r.Id.Equals(Id.Text, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
        // modify the value
        thisRole.Name = Name.Text;
        // edit the role
        context.Entry(thisRole).State = System.Data.Entity.EntityState.Modified;
        // save the role
        context.SaveChanges();
        // get out of the edit mode
        gvRoles.EditIndex = -1;
        // reload the table data
        LoadData();
    }
Prescient
  • 1,051
  • 3
  • 17
  • 42