开发者

Extjs combo box binding from SQl

I am new to ExtJS,i used in asp.net,C# in VS2008.. i simply added comboBox control with static data in aspx page,

but i need to know how to bind the value from sql DB , can any one provide any sample application which explains, 开发者_如何学Chow to get the value from the control and bind to the controls

Thanks in advance


Create a generic HTTP handler, for example our agency list uses this code:

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();
}

In Ext we then do:

    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
            }
        })
    });

Note, we use the 'Json.NET' library to handle JSON output and a custom class, 'StoredProc' to do the database interaction. You also won't have the WriteErrorObject() method that simply serializes out an error, but you get the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜