2009-12-18 68 views
0
結合

我新的ExtJS的,我在asp.net中使用C#在VS2008 ..我簡單地添加在aspx頁面靜態數據ComboBox控件,ExtJS的組合框從SQL

,但我需要知道如何結合來自SQL數據庫的價值,任何一個可以提供解釋,如何從控制和綁定的控件中獲得的價值提前

感謝

回答

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

在內線,我們然後執行:

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()方法,它只是簡單地將一個錯誤序列化,但你明白了。

+0

嗨勞埃德, 我試圖按照我的表字段,但是,我得到一個錯誤--- IQ3是未定義的,所以我刪除該屬性現在它不工作,但沒有錯誤即將到來。我得到Newtonsoft.Json.Net20.dll也... 因爲我沒有extjs的基本知識,我無法識別probelm。 所以,你可以發送示例項目,命中任何常見的SQL數據庫,並獲得數據....我的郵件ID「[email protected]」它非常幫助我完全... – vineth 2009-12-18 12:17:51

+1

在行--JsonWriter json =新的JsonWriter(作家); 我得到了這個錯誤// 無法創建抽象類或接口的實例'Newtonsoft.Json.JsonWriter' – vineth 2009-12-18 12:53:30

+2

我使用的版本是.NET 2.0的舊版本,我相信新版本是JsonTextWriter。 – Lloyd 2009-12-18 13:55:09