怎么样利用ExtJS把数据库的数据取出来再动态绑定到前台的下拉列表。有实例和核心代码最好。

解决方案 »

  1.   

    //前台部分
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="../adapter/ext/ext-base.js"></script>    
        <script type="text/javascript" src="../ext-all.js"></script>
        <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css"/>
    </head>
    <body>
        <form id="frm" runat="server">
        <div id="AreaComboBox" style="margin-left:100px; margin-top:100px"></div>
            <script type="text/javascript" language="javascript">        
                function ready() {
                    var store =new Ext.data.Store(
                    {
                        proxy: new Ext.data.HttpProxy({ method: 'GET', url: 'comboJson.aspx?Flag=1', params: { foo: 'bar'} }),
                        reader: new Ext.data.JsonReader({ totalProperty: "totalProperty", root: "result", fields: [{ name: "PID" }, { name: "username"}] })
                    });
                    store.load();
                    var comboBox = new Ext.form.ComboBox(
                    {
                        id: "ComboBox_ID",                    
                        store: store,                    
                        triggerAction: 'all',
                        valueField: 'PID',
                        displayField: 'username',                   
                        renderTo: 'AreaComboBox',                    
                        resizable: true
                    });
                }
                Ext.onReady(ready);       
            </script>     
        </form>
    </body>
    </html>//comboJson.aspx文件中前台的部分(也就是说comboJson.aspx文件中前台部分html内容全删除了,只剩下以下内容)
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="comboJson.aspx.cs" Inherits="ExtJS.comboJson" %>//comboJson.aspx文件中后台的部分
    protected void Page_Load(object sender, EventArgs e)
    {
                string flag = Request.QueryString["Flag"];           
                switch (flag)
                {
                    case "1":
                        loadCombo("select top 10 PID,username from c_user" );
                        break;
                   default:
                        break;
                }
     }    
    private void loadCombo( string strSql)
    {
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=192.168.1.4;Initial Catalog=PWMIS;User ID=sa;Password=zodiac");
                try
                {
                    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter( strSql , con);
                    con.Open();
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    con.Close();
                    string json = "";
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        json += "[";                    foreach (DataRow row in dt.Rows)
                        {
                            if (json != "[")
                                json += ",";                        string tmp = "{";
                            foreach (DataColumn col in dt.Columns)
                            {
                                if (tmp != "{")
                                    tmp += ",";
                                tmp += string.Format("\"{0}\":\"{1}\"", col.ColumnName, row[col.ColumnName].ToString());
                            }
                            tmp += "}";                        json += tmp;
                        }                    json += "]";                    
                        json = "{totalProperty:10,result:" + json + "}";
                    }
                    else
                    {
                        json = "错误";
                    }
                   
                    Response.Write(json);                
                }
                catch (Exception e)
                {
                    con.Close();
                    throw new Exception(e.Message);                
                }           
    }