实战: asp.net dropdownlist 和 listbox 一起使用

2014-71 应用某门网应用排序管理

 

1: 接收参数, 初始化

 

int totalRows = 7;

int type = 0;

 

protected void Page_Load(object sender, EventArgs e)
  {
            try
            {
                type = Convert.ToInt32(Request.QueryString["f_ID"] == null ? "0" : Request.QueryString["f_ID"]);
                totalRows = Convert.ToInt32((Request.QueryString["columns"] == null ? "7" : Request.QueryString["columns"]));
            }
            catch (Exception)
            {
            }
            finally
            {
                if (!Page.IsPostBack)
                {
                    BindType(type.ToString());
                    BindApp(type.ToString());
                }
            }

  }

 

2: 绑定数据源 (dropdownlist 和 listbox)

 

private void BindType(string type)
  {
            string sqltype = "select id as TypeId , Name as TypeName from  VIEW_DT_WKCommon_LevelDicView where parentid= 4 order by RealOrederNum";

      DataSet typeds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sqltype);

   if (typeds != null && typeds.Tables[0].Rows.Count > 0)
   {
   

    // 数据源绑定方式

    //DataRow r = typeds.Tables[0].NewRow();
    //r["TypeId"] = "0";
    //r["TypeName"] = "默认";

    //typeds.Tables[0].Rows.InsertAt(r, 0);

    //DropDownList1.DataSource = typeds.Tables[0];
    //DropDownList1.DataValueField = "TypeId";
    //DropDownList1.DataTextField = "TypeName";
    //DropDownList1.DataBind();

 

  // 选项添加方式
    DropDownList1.Items.Clear();

    DropDownList1.Items.Add(new ListItem("默认","0"));

    for (int i = 0; i < typeds.Tables[0].Rows.Count; i++)
    {
     ListItem item = new ListItem(typeds.Tables[0].Rows[i]["TypeName"].ToString(), typeds.Tables[0].Rows[i]["TypeId"].ToString());
     DropDownList1.Items.Add(item);
    }

    ListItem it = DropDownList1.Items.FindByValue(type);

    if (it != null)
    {
     DropDownList1.Items.FindByValue(type).Selected = true;
    }
    else
    {
     DropDownList1.SelectedIndex = 0;
    }
    
   }
   else
   {
    ListItem item = new ListItem("无任何分类", "0");
    ListBox1.Items.Add(item);
   }

  }
        
  // 绑定App
  private void BindApp(string type)
  {
   string condition = type == "0" ? string.Format("s.IsDefault=1") : string.Format("a.type={0}", type);

   string sqlapp = string.Format("select s.Id, a.Name, a.Type, s.sort from DT_work_NH_SelfSettings as s, DT_work_NH_SelfDefApps as a where s.appid = a.id and s.CreaterID = {0} and {1} order by sort",UserId, condition);

   DataSet appds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sqlapp);

   if (appds != null && appds.Tables[0].Rows.Count > 0)
   {
    ListBox1.Items.Clear();

    for (int i = 0; i < appds.Tables[0].Rows.Count; i++)
    {
     ListItem item = new ListItem(appds.Tables[0].Rows[i]["Name"].ToString(), appds.Tables[0].Rows[i]["Id"].ToString());
     ListBox1.Items.Add(item);
    }

    InsertLine();
    ListBox1.SelectedIndex = 0;
    lbmsg.Text = string.Format("该分类共有{0}个应用", appds.Tables[0].Rows.Count);
                lbStep.Text = "";
    IsDisenableBtn(false);
   }
   else
   {
    ListBox1.Items.Clear();
    ListBox1.Items.Add("无相关数据项!");
    lbmsg.Text = "提示:该应用分类暂无包含任何应用!";
    lbStep.Text = "";
    IsDisenableBtn(true);
   }
  }

 

3: dropdownlist 数据联动

  // 重新绑定分类信息
  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    BindApp(DropDownList1.SelectedValue);
  }

 

说明:

listbox 有时需要添加额外的选项来区分分组, 比如每4个选项划分为一组,组与组之间添加分隔线

如: ---------------第({0})排---------------

 

  static string line = "---------------第({0})排---------------";

  // 移除每排分隔线
  private void RemoveLine()
  {
   int rows = ListBox1.Items.Count / totalRows;
   int others = ListBox1.Items.Count % totalRows;

   for (int m = 1; m <= rows; m++)
   {
    ListBox1.Items.Remove(new ListItem(string.Format(line, m), "-1"));
   }

   if (others > 0)
   {
    ListBox1.Items.Remove(new ListItem(string.Format(line, rows + 1), "-1"));
   }
  }

 

  // 添加每排分隔线
  private void InsertLine()
  {
   int rows = ListBox1.Items.Count / totalRows;
   int others = ListBox1.Items.Count % totalRows;

   for (int m = 1, n = 0; m <= rows; m++, n++)
   {
    int index = m * Convert.ToInt32(totalRows) + n;
    ListBox1.Items.Insert(index, new ListItem(string.Format(line, m), "-1"));
   }

   if (others > 0)
   {
    ListBox1.Items.Insert(ListBox1.Items.Count, new ListItem(string.Format(line, rows + 1), "-1"));
   }
  }

 

效果如下:

 

 

 

实战: asp.net dropdownlist 和 listbox 一起使用,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。