Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用webforms编辑角色_C#_Asp.net Identity 2 - Fatal编程技术网

C# 使用webforms编辑角色

C# 使用webforms编辑角色,c#,asp.net-identity-2,C#,Asp.net Identity 2,我很难使用webforms设置新的aspnet标识 目前,我正在尝试设置一个管理页面,以便在创建角色后修改它们 到目前为止,我在我的aspx页面上有这个。显示角色的是gridview <asp:GridView ID="gvRoles" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="gvRoles_RowCommand" OnRow

我很难使用webforms设置新的aspnet标识

目前,我正在尝试设置一个管理页面,以便在创建角色后修改它们

到目前为止,我在我的aspx页面上有这个。显示角色的是gridview

<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>
但就我的一生而言,我不知道如何使用context.Entryrole.State中更改的数据更新角色


如果您有任何见解,我们将不胜感激。

我终于明白了

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

角色将仅由具有管理员角色的人员编辑。我需要能够做到这一点,以及分配给不同的角色的人。这是我的下一步,明白了。但这是一项要求。没办法。如果你不想自己滚,有一种方法可以帮你。
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();
    }