使用Web表单编辑角色



我很难使用webforms设置新的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();
    }

但就我的一生而言,我不知道如何在上下文中使用更改后的数据来更新角色。条目(角色)。状态。

任何见解都将不胜感激。

我终于想通了。

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

最新更新