C# 绑定组合框
我正在用Ext.Net开发一个web应用程序 如何从数据库绑定combobox 这是我的疑问:C# 绑定组合框,c#,linq,data-binding,jcombobox,ext.net,C#,Linq,Data Binding,Jcombobox,Ext.net,我正在用Ext.Net开发一个web应用程序 如何从数据库绑定combobox 这是我的疑问: dynamic getRegions = ( from region in db.Regions orderby region.RgnName select region.RgnName); 据我所知,对于Ext.Net.ComboBox您必须使用Ext.Net.Store。例如: <!-- In SamplePage.aspx --> <ext:Res
dynamic getRegions = (
from region in db.Regions
orderby region.RgnName
select region.RgnName);
据我所知,对于
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>
//在SamplePage.aspx.cs中
受保护的无效页面加载(对象发送方、事件参数e)
{
var getRegions=new Dictionary();
getRegions.Add(“Region1”、“英格兰”);
getRegions.Add(“Region2”、“苏格兰”);
getRegions.Add(“Region3”、“威尔士”);
Store1.DataSource=getRegions;
Store1.DataBind();
}
这将生成一个包含单个Ext.Net组合框的页面,其中显示三个值。几乎可以肯定的是,您需要进一步调整它,以获得您想要的内容(因为我不熟悉您的数据库模式),但它应该为您指明正确的方向。出于兴趣,这里有另一个快速的
示例,演示如何在不使用
的情况下向组合框添加数据。基本上与使用
和添加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" />
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!X.IsAjaxRequest)
{
//添加单个项目
这个.ComboBox1.Items.Add(新列表项(“Region1”、“England”);
这个.ComboBox1.Items.Add(新列表项(“Region2”、“Scotland”);
这个.ComboBox1.Items.Add(新列表项(“Region3”、“Wales”);
//AddRange备选方案
//this.ComboBox1.Items.AddRange(新的ListItem[]{
//新列表项(“地区1”、“英格兰”),
//新列表项(“区域2”、“苏格兰”),
//新列表项目(“区域3”、“威尔士”)
// });
}
}
干杯 可能重复:-这可能对您有所帮助。@5arx,不完全是因为Ext.Net控件使用
Ext.Net.Store
作为中介,但数据绑定的一般原则在一定程度上适用,尽管使用Ext.Net(包括动态加载客户端)还有很多其他方法可以实现结果。
<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" />