zoukankan      html  css  js  c++  java
  • GridView内嵌DropDownList操作


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

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

          }
  • 相关阅读:
    C# Xamarin For Android自动升级项目实战
    C# Xamarin移动开发基础进修篇
    .NET轻量级ORM框架Dapper入门精通
    ASP.NET WebApi技术从入门到实战演练
    (简单、可靠的安装方法)在Windows Server2016中安装SQL Server2016
    ASP.NET (Core) WebAPI IIS PUT和DELETE请求失败 405的解决办法
    js中判断对象是否为空的三种实现方法
    windows10如何设置只显示时间不显示日期
    NuGet微软官方中国国内镜像
    Win10找不到hosts文件解决方法
  • 原文地址:https://www.cnblogs.com/sobaby/p/1333083.html
Copyright © 2011-2022 走看看