【问题标题】:Editing roles using webforms使用网络表单编辑角色
【发布时间】:2015-02-20 20:22:34
【问题描述】:

我很难尝试使用网络表单设置新的 aspnet 身份。

目前我正在尝试设置一个管理页面,以便在创建角色后对其进行修改。

到目前为止,我的 aspx 页面中有这个。显示角色的是网格视图。

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

在 gvRoles_RowUpdating 事件中,我有以下代码。

protected void gvRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

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

        gvRoles.EditIndex = -1;

        LoadData();
    }

但对于我来说,我无法弄清楚如何使用 context.Entry(role).State 中更改的数据来更新角色。

任何见解将不胜感激。

【问题讨论】:

  • 角色只能由具有管理员角色的人编辑。我需要能够做到这一点以及将人员分配到不同的角色。这是我的下一步。
  • 明白。但这是一个要求。没办法。
  • 如果您不想自己动手,有一个 bolt on tool 可以为您做到这一点。

标签: c# asp.net-identity-2


【解决方案1】:

我终于明白了。

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();
    }

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2014-07-14
    相关资源
    最近更新 更多