前台有两个个listbox控件,通过listbox控件s1里选中多个选项后移到s2,在点击确定按钮后通过  label1显示s2中的内容,但label1显示为空,不知道问题出在哪里?
protected void Button1_Click(object sender, EventArgs e)
  {
  System.Text.StringBuilder str = new System.Text.StringBuilder();
      foreach (ListItem i in s2.Items)
      {
          if (i.Selected == true)
              str.Append(i.Value + ",");      }      if (str.Length > 2)
          str.Remove(str.Length - 1, 1);
       label1.Text = str.ToString();
  }

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {//两个按妞使用一个实践。COMMANNAME 一个为UP 一个为DOWN
            //声明一个变量INDEX
            //如果满足第一个条件那么INDEX为-1,因为你要实现的功能是像上移动,所以索引要减1
            //如果不满足那就为+1像上移动,因为只有两个按妞来响应一个时间。
            //
            if (((Button)sender).CommandName == "up" && ListBox1.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
            {
                int index;
                if (((Button)sender).CommandName == "up")
                {
                    index = -1;
                }
                else
                {
                    index = 1;
                }
                //声明一个LISTITEM对象把选定的值和文本传进去
                ListItem lt = new ListItem(ListBox1.SelectedItem.Text, ListBox1.SelectedValue);
                //[ListBox1.SelectedIndex]为索引值然后讲ListBox1.Items[ListBox1.SelectedIndex].Text的值
                //传给ListBox1.Items[ListBox1.SelectedIndex + index].Text
                //ListBox1.SelectedIndex + index是经过转换后的索引
                ListBox1.Items[ListBox1.SelectedIndex].Text = ListBox1.Items[ListBox1.SelectedIndex + index].Text;
                ListBox1.Items[ListBox1.SelectedIndex].Value = ListBox1.Items[ListBox1.SelectedIndex + index].Text;
                ListBox1.Items[ListBox1.SelectedIndex + index].Text = lt.Text;
                ListBox1.Items[ListBox1.SelectedIndex + index].Value = lt.Value;
                ListBox1.SelectedIndex = ListBox1.SelectedIndex + index;        }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {    }
        protected void Button3_Click(object sender, EventArgs e)
        {//光标的索引移动到0 也就是第一个位置
            ListBox1.SelectedIndex = 0;
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1;    }
        protected void Button5_Click(object sender, EventArgs e)
        {
            ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1;
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1;
        }
    }
      

  2.   

    不知道问题出在哪里,全部前台的代码如下,当点击按钮后,会正常显示左边listbox控件s1的内容,但当将Button1_Click事件里的s1改为s2,s2为右边listbox控件,则输出的内容为空
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
      <script type="text/javascript">
        var bak = null;
        function setData() {
          ss1 = document.getElementById("<%=s1.ClientID %>");
          ss2 = document.getElementById("<%=s2.ClientID %>");
          if (bak == null) {
            bak = [];
            for (i = 0; i < ss1.length; i++) bak.push(ss1[i].value);
          }      for (i = ss1.length - 1; i > -1; i--) {
            if (ss1[i].selected) {
              ss2.options[ss2.options.length] = new Option(ss1[i].value, ss1[i].value);
              ss1[i].parentNode.removeChild(ss1[i]);
            }
          }
          SortSelect(ss1);
          SortSelect(ss2);
        }    function removeData() {
          ss1 = document.getElementById("<%=s1.ClientID %>");
          ss2 = document.getElementById("<%=s2.ClientID %>");
          for (i = ss2.length - 1; i > -1; i--) {
            if (ss2[i].selected) {
              ss1.options[ss1.options.length] = new Option(ss2[i].value, ss2[i].value);
              ss2[i].parentNode.removeChild(ss2[i]);
            }
          }
          SortSelect(ss1);
          SortSelect(ss2);
        }    function SortSelect(ss) {
          var tt = [];
          for (i = 0; i < bak.length; i++) {
            for (j = 0; j < ss.length; j++) {
              if (ss[j].value == bak[i]) tt.push(bak[i]);
            }
          }
          ss.length = 0;
          for (i = 0; i < tt.length; i++) {
            ss.options[ss.options.length] = new Option(tt[i], tt[i]);
          }
        }
      </script>
    </head>
    <body>
      <form runat="server" id="form1">
      <table>
        <tr>
          <td>
            <asp:ListBox ID="s1" runat="server" SelectionMode="Multiple" Rows="6" 
                  Width="107px">
              <asp:ListItem>A</asp:ListItem>
              <asp:ListItem>B</asp:ListItem>
              <asp:ListItem>C</asp:ListItem>
              <asp:ListItem>D</asp:ListItem>
            </asp:ListBox>
          </td>
          <td>
            <input onclick="setData()" type="button" value="->"/>
            <br />
            <input onclick="removeData()" type="button" value="<-" />
          </td>
          <td>
            <asp:ListBox ID="s2" runat="server" SelectionMode="Multiple" Rows="6" 
                  Width="109px"></asp:ListBox>
          </td>
        </tr>
      </table>
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="gread" />
      
      </form>
      <asp:Label ID="label1" runat="server" ForeColor="Red" Text="123" 
                            Visible="True" style="text-align: right"></asp:Label>                   
    </body>
    </html>
    后台测试:
      protected void Button1_Click(object sender, EventArgs e)
        {
            int t = s1.Items.Count;
            for (int i = 0; i < t; i++)
            {
                Response.Write(s1.Items[i].Text);
                Response.Write("<br/>");
            }
        }