ListBox控件的简单应用-点菜系统

版权声明:Till good is better, but better best !My trying hard will go on!Please wait and see! https://blog.csdn.net/u012761373/article/details/45629137

Page Code:

<table >
          <tr>
            <td rowspan="2"><asp:ListBox ID="lbSourse" runat="server" SelectionMode="Multiple"></asp:ListBox></td>
            <td colspan="2"><asp:Button ID="btnToDest" runat="server" Text=">>>" 
                    onclick="btnToDest_Click" /></td>
            <td rowspan="2"><asp:ListBox ID="lbDest" runat="server" SelectionMode="Multiple"></asp:ListBox></td>
          </tr>
          <tr>
            <td><asp:Button ID="btnToSource" runat="server" Text="<<<" 
                    onclick="btnToSource_Click" /></td>
          </tr>
        </table>

Class Code:

protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				DataInit();
			}
		}
		public void DataInit()
		{
			string strSql = "select c_code,c_name from studentInfo where c_db_status=0 order by c_code";
			DataSet ds = DS(strSql);
			lbSourse.DataSource = ds.Tables[0].DefaultView;
			lbSourse.DataTextField = ds.Tables[0].Columns["c_name"].ToString();
			lbSourse.DataValueField = ds.Tables[0].Columns["c_code"].ToString();
			lbSourse.DataBind();
			lbSourse.SelectedIndex = 0;
		}
		public DataSet DS(string strSql)
		{
			string strConnection = ConfigurationManager.AppSettings["MySqlDataBase"].ToString();
			//string strConnection = ConfigurationManager.ConnectionStrings["MyTest"].ConnectionString;
			//string strSql = "select * from studentInfo where c_name like '%" + Session["Key"].ToString() +"%'";
			//string strSql = "SELECT * FROM (SELECT row_number() over(order by Gs.PtypeId) AS rw,Gs.Qty as 数量,Gs.Price as 单价,Gs.Total as 总金额,Pt.* FROM GoodsStocks Gs left join ptype as Pt on Pt.ptypeid=Gs.PtypeId ) t WHERE rw between 1 AND 10;";
			SqlConnection conn = new SqlConnection(strConnection);
			conn.Open();
			//SqlCommand cmd = new SqlCommand();
			SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
			conn.Close();
			DataSet ds = new DataSet();
			da.Fill(ds);
			return ds;
		}

		protected void btnToDest_Click(object sender, EventArgs e)
		{
			int count = lbSourse.Items.Count;
			int index = 0;
			for (int i = 0; i < count; i++)
			{
				ListItem item = lbSourse.Items[index];
				if (item.Selected == true)
				{
					lbSourse.Items.Remove(item);
					lbDest.Items.Insert(0, item);//添加的元素显示在第一行
					//lbDest.Items.Add((item));
					index--;
				}
				index++;
			}
		}

		protected void btnToSource_Click(object sender, EventArgs e)
		{
			if (lbDest.Items.Count == 0)
			{
				Response.Write("请点菜");
			}
			else
			{
				int count = lbDest.Items.Count;
				int index = 0;
				for (int i = 0; i < count; i++)
				{
					ListItem item = lbDest.Items[index];
					if (item.Selected == true)
					{
						lbDest.Items.Remove(item);
						lbSourse.Items.Insert(0, item);//添加的元素显示在第一行
						//lbSourse.Items.Add(item);
						index--;
					}
					index++;
				}
			}
		}


猜你喜欢

转载自blog.csdn.net/u012761373/article/details/45629137