zoukankan      html  css  js  c++  java
  • DataGrid中DropDownList的动态绑定和触发DropDownList事件

    我在写DataGrid控件中子控件事件时候,DropDownList的事件相比而言麻烦一点,在此,我简单罗列如下(我在此处为了方便这里都用DataGrid中的隐藏列存储我所要的数据):

    一、DropDownList的动态绑定,只需在DataGrid1_ItemDataBound的事件中,取出数值进行匹配,便可以了,具体代码如下:
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
      {
       if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem)) 
       {
        string StrPower = e.Item.Cells[3].Text; // 用隐藏列取出数据
        DropDownList DrpRole = (DropDownList)e.Item.Cells[4].FindControl("DrplRole");
        for(int i=0;i<DrpRole.Items.Count;i++)
        {
         if(StrPower.Equals(DrpRole.Items[i].Value))
         {
          DrpRole.Items[i].Selected = true;

         }
        }    
       }
      }

    二、触发DataGrid中DropDownList的事件

    前台:

    <asp:TemplateColumn HeaderText="分配角色">
            <ItemTemplate>
             <asp:DropDownList id="DrplRole" runat="server" Width="120px" OnSelectedIndexChanged="DrplRole_SelectedIndexChanged"
              AutoPostBack="True">
              <asp:ListItem Value="未分配权限">未分配权限</asp:ListItem>
              <asp:ListItem Value="开单员">开单员</asp:ListItem>
              <asp:ListItem Value="维护员">维护员</asp:ListItem>
             </asp:DropDownList>
            </ItemTemplate>
           </asp:TemplateColumn>

    注:  AutoPostBack="True" 千万不能落下


    后台:

    public void DrplRole_SelectedIndexChanged(object sender, System.EventArgs e)
      {
       // 选择用户类型   semir.customClass.database dbnew = new semir.customClass.database();
       DropDownList DrplRole = (DropDownList)sender;
       TableCell cell = (TableCell)DrplRole.Parent;
       DataGridItem item = (DataGridItem)cell.Parent;

       string StrPower = ((DropDownList)sender).SelectedItem.ToString(); // 取出DropDownList选中项文本
       int ITid = Convert.ToInt32(item.Cells[0].Text);    // 取出该行的第一格的数据(主键)
       string sqlUpPower = "update ITUsers set ITPower='"+StrPower+"' where ITID='"+ITid+"'";
       dbnew.DatabaseCommand(sqlUpPower); // 数据库操作

       BindDataUser(); // 绑定数据    
      }



  • 相关阅读:
    虚拟机安装RHEL8.0.0
    给KVM添加新的磁盘
    RedHat7.4安装在个人电脑(笔记本)中安装遇到的问题总结
    shell编程-ssh免交互批量分发公钥脚本
    Error:Connection activation failed: No suitable device found for this connection 问题最新解决方案
    Linux下系统防火墙的发展历程和怎样学好防火墙(iptalbes和firewalld)
    Linux bash命令行常用快捷键(Xshell和secure CRT以及gnome-terminal)
    编写mysql多实例启动脚本
    RHEL7配置端口转发和地址伪装
    java中的事务,四大特性,并发造成的问题,隔离级别
  • 原文地址:https://www.cnblogs.com/skylaugh/p/352333.html
Copyright © 2011-2022 走看看