我很难尝试使用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();
}
但是对于我的生活,我无法弄清楚如何使用context.Entry(role).State中的已更改数据更新角色。
任何见解都将受到赞赏。
答案 0 :(得分:0)
我终于明白了。
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();
}