效果:
步骤:
1.先把DropDownList所在的列转换成TemplateField(模板列)
2.在GridView里添加三个事件OnRowEditing,OnRowCancelingEdit,OnRowUpdating
再在事件上写上相应的代码
代码片段:
ASPX文件:
<asp:GridView ID="gvRequestRoleList" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowPaging="True" OnPageIndexChanging="gvRequestRoleList_PageIndexChanging"
OnRowEditing="gvRequestRoleList_RowEditing" OnRowCancelingEdit="gvRequestRoleList_RowCancelingEdit"
OnRowUpdating="gvRequestRoleList_RowUpdating">
.......
<asp:TemplateField HeaderText="申请权限">
<EditItemTemplate>
<asp:DropDownList ID="ddlRequestRole" runat="server">
<asp:ListItem Value="Common">普通用户</asp:ListItem>
<asp:ListItem Value="Intermediate">中级用户</asp:ListItem>
<asp:ListItem Value="Senior">高级用户</asp:ListItem>
<asp:ListItem Value="Admin">超级用户</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RequestRoleCHName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
.......
</asp:GridView>
CS文件:
//用户按"修改权限"(更新)时
protected void gvRequestRoleList_RowEditing(object sender, GridViewEditEventArgs e)
{
gvRequestRoleList.EditIndex = e.NewEditIndex;
//绑定数据
RequestRoleListDataBind();
}
//用户取消操作时
protected void gvRequestRoleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvRequestRoleList.EditIndex = -1;
//绑定数据
RequestRoleListDataBind();
}
//更新操作
protected void gvRequestRoleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//获得DropDownList 控件
DropDownList ddlRequestRole =
gvRequestRoleList.Rows[e.RowIndex].Cells[5].FindControl("ddlRequestRole")
as DropDownList;
string roleName = ddlRequestRole.SelectedItem.Value;
Label lblUserName = gvRequestRoleList.Rows[e.RowIndex].Cells[2].FindControl("lblUserName") as Label;
string userName = lblUserName.Text;
//更新用户的角色
ChangeRole(userName, roleName);
//审核用户的申请
//....(略)
//刷新页面
//....(略)
}