<select id="Select1">
        <option></option>
    </select>例如我在后天页面生成啦一个数组,请问 我怎么样用javascript把数组和下拉框控件连接在一起!

解决方案 »

  1.   

    通过JS回调获取数组的信息,将数据信息拼凑成一个字符串,传回客户端后再解析绑定上去
    给一个客户回调的例子源码
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallBack_Default.aspx.cs" Inherits="CallBack_Default" %><!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 runat="server">
        <title>使用.net回调(CallBack)技术实现局部刷新</title>
        <script language="javascript" type="text/javascript">
            function FillData()
            {
                var city = document.getElementById("TextBox1").value;
                <% =ClientScript.GetCallbackEventReference(this,"city","FillDll",null) %>;
            }
            
             function FillDll(strCity)
            {
               document.getElementById("DropDownList1").options.length = 0;
               var indexofcity;
               var city;
               //分割传递来的字符串
               while(strCity.length>0)
               {
                 //判断是否是最后一个字符串
                 indexofcity = strCity.indexOf(",");
                 if (indexofcity > 0)
                 { 
                   city = strCity.substring(0,indexofcity);
                   strCity = strCity.substring(indexofcity+1);
                   //填充DropDownList1
                   document.getElementById("DropDownList1").add(new Option(city,city));
                 }
                 else
                 {
                   document.getElementById("DropDownList1").add(new Option(strCity,strCity));
                   break;
                 }
               }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <strong>使用.net回调(CallBack)技术实现局部刷新</strong><br />
            城市名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <input id="Button1" type="button" value="查 询" onclick="FillData()" /><br />
            城镇:<asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList></div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class CallBack_Default : System.Web.UI.Page,ICallbackEventHandler
    {
        private string _data;    protected void Page_Load(object sender, EventArgs e)
        {    }    public string GetCallbackResult()
        {
            return _data;
        }    public void RaiseCallbackEvent(string eventArgument)
        {
            switch (eventArgument)
            {
                case "武汉":
                    _data = "武昌,汉口,洪山";
                    break;
                case "钟祥":
                    _data = "郢中,胡集,双河";
                    break;
                default:
                    break;
            }
        }}--声明例子非原创,作者记不得了
      

  2.   

    var arr = new Array[];
    .....
    document.getElementById("DropDownList1").add(new Option(i.toString(),arr[i].toString()));
      

  3.   

    楼上应该是正解!我前段时间直接用在后台执行JS给<select id="Select1"> </select> 直接填充html内容如下
    做个循环把数组元素 按 stringbuilder sb.append("<option value="数组值">数组值</option>"); 
     
     然后执行以下"<script>document.getElementByID('select1').innerHtml="+sb.tostring+"</script>";
      

  4.   


    up参考:
    http://shirlly.javaeye.com/blog/351665
      

  5.   

    .aspxvar arr = "<%=str%>".split('|');
    var sel = document.getElementById("Select1");
    for(var i=0;i<arr.length;i++){
    sel.options[i] = new Option(arr[i],arr[i]);
    }.aspx.csprotected string str;
    page_load
    {
    string[] strs = {"a","b","c"};
    str = String.Join(strs,"|");
    }
      

  6.   

    直接<%= %>调用后台的属性
    或者利用ajax
      

  7.   

    后台定义个数组 如果闲麻烦 直接 Public xx类型的数组a
    前台就<option><%=a%></option>在HTML标记里直接嵌套后台代码就OK了 
    这样写不太好的不过能容易满足自己变态的欲望