2011-02-15 93 views
2

我正在開發一個帶有Ext.Net的Web應用程序。Binding Combobox

如何從數據庫中綁定組合框?

這是我的查詢:

dynamic getRegions = (
    from region in db.Regions 
    orderby region.RgnName 
    select region.RgnName); 
+1

可能重複覆蓋的材料:http://stackoverflow.com/questions/1817403/binding-a-combobox-using-a-datacontext-and-linq-to-sql - 這可能會幫助你。 – 5arx 2011-02-15 16:56:33

回答

0

出於興趣的原因,下面是另一個快速<ext:ComboBox>示例,它演示瞭如何在不使用<ext:Store>的情況下向ComboBox添加數據。基本上與使用<asp:DropDownList>並添加ListItem對象的技術相同。

<script runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!X.IsAjaxRequest) 
     { 
      // Add individual Items 
      this.ComboBox1.Items.Add(new ListItem("Region1", "England")); 
      this.ComboBox1.Items.Add(new ListItem("Region2", "Scotland")); 
      this.ComboBox1.Items.Add(new ListItem("Region3", "Wales")); 

      // AddRange alternative 
      // this.ComboBox1.Items.AddRange(new ListItem[] { 
      //  new ListItem("Region1", "England"), 
      //  new ListItem("Region2", "Scotland"), 
      //  new ListItem("Region3", "Wales") 
      // }); 
     } 
    } 
</script> 

<ext:ComboBox ID="ComboBox1" runat="server" /> 

乾杯!

2

你必須,對於Ext.Net.ComboBox使用Ext.Net.Store據我所知。例如:

<!-- In SamplePage.aspx --> 
<ext:ResourceManager runat="server"></ext:ResourceManager> 
<ext:Store runat="server" ID="Store1">   
    <Reader> 
     <ext:JsonReader IDProperty="Value"> 
      <Fields> 
       <ext:RecordField Name="Key" /> 
       <ext:RecordField Name="Value" /> 
      </Fields> 
     </ext:JsonReader> 
    </Reader> 
</ext:Store> 

<ext:ComboBox runat="server" ID="myCombo" StoreID="Store1" 
    DisplayField="Key" ValueField="Value"> 
</ext:ComboBox> 

 

// In SamplePage.aspx.cs 
protected void Page_Load(object sender, EventArgs e) 
{ 
    var getRegions = new Dictionary<string, string>(); 
    getRegions.Add("Region1", "England"); 
    getRegions.Add("Region2", "Scotland"); 
    getRegions.Add("Region3", "Wales"); 

    Store1.DataSource = getRegions; 
    Store1.DataBind(); 
} 

這導致包含具有顯示三個值單Ext.Net組合框的頁面。你幾乎可以肯定需要進一步調整,以獲得你究竟是什麼(因爲我不熟悉你的數據庫模式),但它應該指向你在正確的方向。