日期:2014-05-17  浏览次数:20768 次

再一次,ListBox控件,代码不懂
protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  lbxSource.Items.Add("星期日");
  lbxSource.Items.Add("星期一");
  lbxSource.Items.Add("星期二");
  lbxSource.Items.Add("星期三");
  lbxSource.Items.Add("星期四");
  lbxSource.Items.Add("星期五");
  lbxSource.Items.Add("星期六");


  }
  }

   
  protected void Button1_Click(object sender, EventArgs e)
  { 
  //若不是第一行则上移
  if (lbxSource.SelectedIndex > 0 && lbxSource.SelectedIndex <= lbxSource.Items.Count - 1)
  {
  //记录当前选项的值
  string name = lbxSource.SelectedItem.Text;这里的name是不是指"星期几"?
  string value = lbxSource.SelectedItem.Value;这里的value指的是什么值?
  //获取当前选项的索引号
  int index = lbxSource.SelectedIndex;什么是索引号?
  //交换当前选项和其前一项的索引号
  lbxSource.SelectedItem.Text = lbxSource.Items[index - 1].Text;
  lbxSource.SelectedItem.Value = lbxSource.Items[index - 1].Value;
  lbxSource.Items[index - 1].Text = name;
  lbxSource.Items[index - 1].Value = value;
  //设定上一项为当前选项
  lbxSource.SelectedIndex--;

  }

------解决方案--------------------
name和value可以是不同的值,不过你这里应该是一样的,索引号就是你选择的项目在列表中是第几项,第一项的索引是0
------解决方案--------------------
name 一般就是你 显示的名称, 也就是 星期几,value 就是星期几对应的值,你在网页源代码里能看到的。

索性就是list的编号,一般是从 0开始的
------解决方案--------------------
1、lbxSource.Items.Add(new ListItem("text星期日", "value星期日"));//以这种方式添加item的话,lbxSource.SelectedItem.Text 取的是text星期日;lbxSource.SelectedItem.Value 取的是value星期日
2、bxSource.SelectedIndex 是索引,在C#中索引一般是从0开始的,比如获得"星期六"item的SelectedIndex就是6。