• 在DropDownList控件中动态绑定多级部门


    在.aspx页面中添加DropDownList控件:

    <asp:DropDownList ID="ddlDep" runat="server">
    </asp:DropDownList>

    在.cs中添加以下代码:

    /// <summary>
    /// 部门下拉框数据绑定
    /// </summary>
    private void DropDownBind()
    {
      //部门绑定   DataView depdv = GetTable("-2").DefaultView;//通过GetTable方法来获得部门父级ID为-2的所有部门   DDlTypeBind(ddlDep, depdv, "DepID", "DepName", "-3", 1); }
    /// <summary>
    /// 数据源绑定下拉框
    /// </summary> /// <param name="ddlControl">控件名称</param> /// <param name="dv">数据源</param> /// <param name="valuename">绑定值</param> /// <param name="textname">显示字段</param> /// <param name="firstvalue">请选择默认值</param> /// <param name="flag">值为-1:不绑定子部门;其他:绑定子部门</param> public static void DDlTypeBind(DropDownList ddlControl, DataView dv, string valuename, string textname, string firstvalue, int flag) {   ddlControl.Items.Clear();   ddlControl.Items.Add(new ListItem("--请选择--", firstvalue));   if (dv != null && dv.Table.Rows.Count > 0)   {     foreach (DataRow row in dv.Table.Rows)     {       ddlControl.Items.Add(new ListItem(row[textname].ToString(), row[valuename].ToString()));       if (flag != -1)       {         DDlTypeChildBind(ddlControl, valuename, textname, row[valuename].ToString(), 0);       }     }   } }
    /// <summary>
    /// 子部门数据绑定
    /// </summary>
    /// <param name="ddlControl">控件名称</param>
    /// <param name="valuename">绑定值</param>
    /// <param name="textname">显示字段</param>
    /// <param name="ddlpid">父级ID</param>
    /// <param name="level">层次</param>
    public static void DDlTypeChildBind(DropDownList ddlControl, string valuename, string textname, string ddlpid, int level)
    {
      DataView dv = new DataView();
      dv = GetTable(ddlpid).DefaultView;
      level++;   string str = "";   switch (level)   {     case 1:       str = " ☆";       break;     case 2:       str = "  ◇";       break;     case 3:       str = "   ◎";       break;     case 4:       str = "    ⊙";       break;     case 5:       str = "     ○";       break;     default:       break;   }   if (dv != null && dv.Count > 0)   {     foreach (DataRow dr in dv.Table.Rows)     {       ddlControl.Items.Add(new ListItem(str + dr[textname].ToString(), dr[valuename].ToString()));       DDlTypeChildBind(ddlControl, valuename, textname, dr[valuename].ToString(), level);     }   } }
  • 相关阅读:
    MYSQL-----IFNULL()函数的用法
    《你的灯亮着吗?》读书笔记
    《测试架构师修炼之路》阅读笔记第三章
    未记录书名的一本营销书阅读记录
    Spring注解@Component、@Repository、@Service、@Controller区别
    PropertyPlaceholderConfigurer的用法:
    Jackson Streaming API to read and write JSON
    ParameterizedType的作用
    setTimeOut传参数(转)
    mybatis使用count返回int的方法
  • 原文地址:https://www.cnblogs.com/ahhswyf/p/3433667.html
Copyright © 2020-2023  润新知