繁体   English   中英

来自SQl的Extjs组合框绑定

[英]Extjs combo box binding from SQl

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

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

提前致谢

创建一个通用的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()方法,但是您可以理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM