I am developing a web applic开发者_运维百科ation with Ext.Net.
How can I bind combobox from database?
This is my query:
dynamic getRegions = (
from region in db.Regions
orderby region.RgnName
select region.RgnName);
You have to, for an Ext.Net.ComboBox
use an Ext.Net.Store
as far as I'm aware. For example:
<!-- 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();
}
This results in a page that contains a single Ext.Net combobox that has three values displayed. You'll almost certainly need to tweak this further to get exactly what you're after (as I'm not familiar with your database schema), but it should point you in the right direction.
Just out of interests sake, here's another quick <ext:ComboBox>
sample which demonstrates adding data to the ComboBox without using an <ext:Store>
. Basically the same technique as using an <asp:DropDownList>
and adding ListItem objects.
Example
<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" />
Cheers!
http://en.csharp-online.net/DataViews_and_Data_Binding%E2%80%94Single-Value_Binding
精彩评论