GridView内嵌DropDownList操作

这篇博客介绍了如何在GridView中实现内嵌DropDownList的编辑功能。通过将DropDownList所在的列转换为TemplateField,然后添加OnRowEditing、OnRowCancelingEdit和OnRowUpdating事件,实现了用户权限级别的选择和更新。在CS文件中,详细展示了处理这些事件的代码,包括获取选中值、更新数据和页面刷新等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果:
 

步骤:
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);         
       
          //审核用户的申请
         //....(略)

         //刷新页面
          //....(略)

      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值