来自SQl的Extjs组合框绑定

时间:2009-12-18 09:05:49

标签: c# asp.net extjs

我是ExtJS的新用户,我用于asp.net,VS2008中的C#..我只是在aspx页面中添加了带静态数据的comboBox控件,

但我需要知道如何绑定sql DB中的值,任何人都可以提供任何解释的示例应用程序,如何从控件中获取值并绑定到控件

提前致谢

1 个答案:

答案 0 :(得分:2)

创建通用HTTP处理程序,例如我们的代理商列表使用以下代码:

public void ProcessRequest(HttpContext context)
{
       context.Response.ContentType = "text/javascript";
       context.Response.ContentEncoding = Encoding.UTF8;

       // Get User ID
        int user_id;

        try {
            user_id = int.Parse(context.Session["user_id"].ToString());
        } catch {
            WriteErrorObject(context,"Could not find required user in the session.");
            return;
        }

        // Get Query
        string query;

        try {
            query = context.Request.QueryString["query"];

            if (String.IsNullOrEmpty(query)) throw new Exception();
        } catch {
            query = "";
        }

        // Get Revision
        int revision;

        try {
            revision = int.Parse(ConfigurationManager.AppSettings["reportingRevision"]);
        } catch {
            revision = -1;   
        }

        // Check for our connection string
        try {
            if (ConfigurationManager.ConnectionStrings["reportInstance"] == null) throw new Exception(); 
        } catch {
            WriteErrorObject(context,"Cannot find the database connection string.");
            return;
        }

        // Get our connection string
        string connectionstring = ConfigurationManager.ConnectionStrings["reportInstance"].ConnectionString;

        // Create our sproc caller
        StoredProc proc;

        try {
            proc = new StoredProc("usp_rep2_agency_list",connectionstring,30);
        } catch (Exception ex) {
            WriteErrorObject(context,"There was an exception creating the stored procedure caller: " + ex.Message);
            return;
        }

        // Set up sproc
        if (revision != -1) proc.AddParameter("@revision",revision,SqlDbType.Int);

        proc.AddParameter("@user_id",user_id,SqlDbType.Int);

        if (query != null && query.Length > 0) proc.AddParameter("@query",query,SqlDbType.NVarChar);

        // Execute sproc
        DataSet results;

        try {
            results = (DataSet)proc.Execute(StoredProc.ExecuteTypes.ReturnDataset);
        } catch (Exception ex) {
            WriteErrorObject(context,"There was an exception calling the stored procedure: " + ex.Message);
            return;   
        }

        // Check we have results
        if (results == null) {
            WriteErrorObject(context,"There was no dataset returned from the stored procedure.");
            return;   
        }

        // Check we have a table
        if (results.Tables.Count < 1) {
            WriteErrorObject(context,"There was no tables found in the returned dataset from the stored procedure.");
            return;   
        }

        // Get the table
        DataTable table = results.Tables[0];

        // Begin JSON
        StringWriter writer = new StringWriter();
        JsonWriter json = new JsonWriter(writer);

        json.WriteStartObject();
        json.WritePropertyName("success");
        json.WriteValue(true);
        json.WritePropertyName("count");
        json.WriteValue(table.Rows.Count);
        json.WritePropertyName("list");
        json.WriteStartArray();

        // Process table rows
        for (int i = 0; i < table.Rows.Count; i++) {
            // Get row
            DataRow row = table.Rows[i];

            // ID
            if (row["agency_id"] == null || row["agency_id"] == DBNull.Value) {
                WriteErrorObject(context,"There was an error processing the agency id value from row " + i.ToString() + ".");
                return;
            }

            int agency_id;

            if (!int.TryParse(row["agency_id"].ToString(),out agency_id)) {
                WriteErrorObject(context,"Could not parse the agency id value from row " + i.ToString() + ".");
                return;   
            }

            // Name
            if (row["agency_name"] == null || row["agency_name"] == DBNull.Value) {
                WriteErrorObject(context,"There was an error processing the agency name value from row " + i.ToString() + ".");
                return;   
            }

            string agency_name = row["agency_name"].ToString();

            // Write out JSON for this row
            json.WriteStartObject();
            json.WritePropertyName("agency_id");
            json.WriteValue(agency_id);
            json.WritePropertyName("agency_name");
            json.WriteValue(agency_name);
            json.WritePropertyName("icon");
            json.WriteValue("iq-reporting-dropdowns-agency");
            json.WriteEndObject();
        }

        // End JSON
        json.WriteEndArray();
        json.WriteEndObject();

        string text = writer.GetStringBuilder().ToString();

        context.Response.Write(text);
        context.Response.Flush();
}

在Ext中我们做:

    this.ddlAgency = new Ext.form.ComboBox({
        fieldLabel: "Agency",
        mode: "remote",
        triggerAction: "all",
        forceSelection: true,
        displayField: "agency_name",
        valueField: "agency_id",
        iconField: "icon",
        typeAhead: true,
        minChars: 1,
        allowBlank: false,
        anchor: "100%",
        emptyText: "Select an Agency...",
        store: new Ext.data.Store({
            autoLoad: false,
            proxy: new Ext.data.HttpProxy({
                method: "GET",
                url: "whatever.ashx"
            }),
            reader: new Ext.data.JsonReader(
                {root: "list", totalProperty: "count"}, 
                [{name: "agency_id", type: "int"},{name: "agency_name", type: "string"},{name: "icon", type: "string"}]
            ),
            baseParams: {
                action: "agencylist",
                level: 1
            }
        })
    });

注意,我们使用'Json.NET'库来处理JSON输出和一个自定义类'StoredProc'来进行数据库交互。你也没有WriteErrorObject()方法只是序列化一个错误,但你明白了。