本帖最后由 hanyise520 于 2010-07-16 01:00:29 编辑

解决方案 »

  1.   

    这个可以用服务器控件<asp:ListBox runat="server" id="mylist" SelectionMode="Multiple" />
    后台:
       StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mylist.Items.Count; i++)
    {   if (mylist.Items[i].Selected)
       {
           sb.Append(mylist.Items[i].Value + ",");
       }
    }
      

  2.   

    javascript实现
    C#后台不能获得值,不管有没有runat="server",但request是可以获得值的
    我刚找的资料,且在机器上运行了,试了JS
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>选择下拉菜单--http://www.yongfa365.com/item/686ba37800f67121.html</TITLE>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <META NAME="Description" CONTENT="Power by 51windows.Net">
    </HEAD>
    <BODY>
    <form method="post" name="myform">
      <table border="1" width="400">
        <tr>
          <td><CENTER>
              可选择排序方式
            </CENTER></td>
          <td></td>
          <td><CENTER>
              已选择排序方式
            </CENTER></td>
        </tr>
        <tr>
          <td width="40%"><select style="width:100%;" multiple name="left" size="8"
      ondblclick="moveOption(document.getElementByIdx('left'), document.getElementByIdx('right'))">
              <option value="20">按编入时间(晚到早)</option>
              <option value="30">按编入时间(早到晚)</option>
              <option value="40">按点击率(升序)</option>
              <option value="50">按点击率(降序)</option>
              <option value="60">按资产名称(升序)</option>
              <option value="70">按资产名称(升序)</option>
            </select>
          </td>
          <td width="20%" align="center"><input type="button" value=" >>>> " onClick="moveOption(document.getElementByIdx('left'), document.getElementByIdx('right'))">
            <br>
            <br>
            <input type="button" value=" <<<< " onClick="moveOption(document.getElementByIdx('right'), document.getElementByIdx('left'))">
          </td>
          <td width="40%"><select style="width:100%;" multiple name="right" size="8"
      ondblclick="moveOption(document.getElementByIdx('right'), document.getElementByIdx('left'))">
            </select>
          </td>
        </tr>
        <tr>
          <td colspan="3"><CENTER>
              <INPUT TYPE="button" value="优先级上升" onClick="changepos(right,-1)">
              <INPUT TYPE="button" value="优先级下降" onClick="changepos(right,1)">
              <INPUT TYPE="button" value="优先级置顶" onClick="gototop(right)">
            </CENTER></td>
        </tr>
      </table>
      值:
      <input type="text" name="result" id="result" size="40" value="" />
    </form>
    <script language="JavaScript">
    <!--
    function moveOption(e1, e2){
     for(var i=0;i<e1.options.length;i++){
      if(e1.options[i].selected){
       var e = e1.options[i];
       e2.options.add(new Option(e.text, e.value));
       e1.remove(i);
       i=i-1
      }
     }
     document.getElementByIdx('result').value=getvalue(document.getElementByIdx('right'));
    }
    function getvalue(geto){
     //var allvalue = "";
     //for(var i=0;i<geto.options.length;i++){
     // allvalue +=geto.options[i].value + ",";
     //}
     var resultArray = new Array();
     for(var i=0;i<geto.options.length;i++){
      resultArray.push(geto.options[i].value);
     }
     return resultArray.join();
    }
    function changepos(obj,index){
     if(index==-1){
      if(obj.selectedIndex !=-1){
       if (obj.selectedIndex>0){
        obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex-1));
        document.getElementByIdx('result').value=getvalue(document.getElementByIdx('right'));
       }
      }else{
       alert("请至少选择一个已选择排序方式");
      }
     }else if(index==1){
      if(obj.selectedIndex !=-1){
       if (obj.selectedIndex<obj.options.length-1) {
        obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex+1));
        document.getElementByIdx('result').value=getvalue(document.getElementByIdx('right'));
       }
      }else{
       alert("请至少选择一个已选择排序方式");
      }
     }
    }
    function gototop(obj){
     if(obj.multiple){
      if(obj.selectedIndex !=-1){
       for(var selIndex=0; selIndex<obj.options.length; selIndex++){
        if(obj.options[selIndex].selected){
                         var transferIndex = selIndex;
                         while(transferIndex > 0 && !obj.options[transferIndex - 1].selected){
                             obj.options[transferIndex].swapNode(obj.options[transferIndex - 1]);
                             transferIndex --;
                         }
          document.getElementByIdx('result').value=getvalue(document.getElementByIdx('right'));
                     }
       }
      }else{
       alert("请至少选择一个已选择排序方式");
      }
     }else{
      if(obj.selectedIndex !=-1){
       var selIndex = obj.selectedIndex;
       while(selIndex > 0){
        obj.options[selIndex].swapNode(obj.options[selIndex - 1]);
        selIndex --;
       }
       document.getElementByIdx('result').value=getvalue(document.getElementByIdx('right'));
      }else{
       alert("请至少选择一个已选择排序方式");
      }
     }
    }
    //-->
    </script>
    </BODY>
    </HTML>
      

  3.   

    用客户端脚本加载的选项服务器端控件是无法获取的,但用Request.Form完全可以,之所以楼主取不到,在于你不理解多选框的特性:它只提交被选中的项的值。在提交表单前,你可以用脚本将所有项选中,这样用Request.Form就可以取到值了。
      

  4.   

    2楼大哥,我是用jQuery 动态改变了select2 的 option ,用了你的方法,在后台还是获取不到,我看你的这个是 只有一个select 
      

  5.   

    还有我在问个小问题,我怎么 给时间类型的字段 赋空值? model.addTime=null; 每次在插入数据库都说 不行
      

  6.   

    <select multiple="multiple" id="select2"  style="width:100px;height:160px;">=><select multiple="multiple" id="select2" name='select2Name'  style="width:100px;height:160px;">
                
                
    Request.Form["select2Name"]DateTime ?
      

  7.   

    哥明白了,如 陕北吴旗娃 所说 要选中才行。我现在在想,怎么在提交之前让他全部都选中??datetime? 这个解决了。谢谢
      

  8.   

    哦,恍然大悟。要设置 name 而不是ID ,哎,用服务器控件用多了。  提交前全部选中是写 JS 全部遍历选中 select2 里的 选项吗?
      

  9.   

    就判断select2.value 是否等于空
      

  10.   

    $('#btnSubmit').click(function(){
       $("select2").children().each(function(){$(this).attr("selected","selected")})
    });
    我这样写了 后台获取 还是 没有选中全部
      

  11.   

    $("select2").children().each(function(){$(this).attr("selected","selected")})=>$("#select2 option").attr("selected","selected");
      

  12.   

    $("select2").children().each(function(){$(this).attr("selected","selected")})
    $("#select2")
      

  13.   

    哦,新问题来了。
    我用了updatepanel 在提交之后 再提交就 不能全部选中了 <asp:UpdatePanel ID="up_submit" runat="server">
                            <contenttemplate>
                    <asp:Button ID="btnSubmit" runat="server" Text="确认提交" CssClass="btn" OnClick="btnSubmit_Click" />
                 </contenttemplate>
                        </asp:UpdatePanel>
      

  14.   

    我不使用UpdatePanel  ,现在判断不出是哪里出了问题。有更详细的信息么
      

  15.   


    现在能知道的就是 在提交之后 再提交 <asp:Button ID="btnSubmit" runat="server" Text="确认提交" CssClass="btn" OnClick="btnSubmit_Click" /> 应该是不执行 jQuery 全部选中那段代码了  
                 
      

  16.   

    那你设置 Button  的 OnClientClick 属性OnClientClick='$("#select2 option").attr("selected","selected");'