我在ASP.NET 4.0中使用Ext.Net 1.3 我想用C#动态生成ComboBox和Store,以下是我的代码。
var data = new object[]
{
new object[]{"AL", "Alabama", "The Heart of Dixie"},
new object[] { "AK", "Alaska", "The Land of the Midnight Sun"},
new object[] { "AZ", "Arizona", "The Grand Canyon State"},
new object[] { "AR", "Arkansas", "The Natural State"},
new object[] { "CA", "California", "The Golden State"},
new object[] { "CO", "Colorado", "The Mountain State"},
new object[] { "CT", "Connecticut", "The Constitution State"}
};
Ext.Net.ComboBox cmb = new Ext.Net.ComboBox();
cmb.TypeAhead = true;
cmb.ForceSelection = true;
cmb.DisplayField = "ItemCode";
cmb.ValueField = "ItemName";
cmb.MinChars = 1;
cmb.ListWidth = 400;
cmb.PageSize = 10;
cmb.ItemSelector = "tr.list-item";
Store s = new Store();
s.AddField(new RecordField() { Name = "ItemCode", Type = RecordFieldType.String }, 0);
s.AddField(new RecordField() { Name = "ItemName", Type = RecordFieldType.String }, 1);
s.AddField(new RecordField() { Name = "OnHand", Type = RecordFieldType.String }, 2);
s.SaveAllFields = true;
s.DataSource = data;
s.DataBind();
cmb.Store.Add(s);
StringBuilder sHtml = new StringBuilder();
sHtml.Append(" <tpl for=\".\"><tpl if=\"[xindex] == 1\">");
sHtml.Append("<table class=\"cbStates-list\" ><tr>");
sHtml.Append("<th style=\"color: #2f353b !important;\">ItemCode</th>");
sHtml.Append(" <th style=\"color: #2f353b !important;\">ItemName</th>");
sHtml.Append("<th style=\"color: #2f353b !important;\">OnHand</th>");
sHtml.Append("</tr> </tpl>");
sHtml.Append("<tr class=\"list-item\">");
sHtml.Append("<td style=\"padding:3px 0px;\">{ItemCode}</td>");
sHtml.Append("<td>{ItemName}</td>");
sHtml.Append("<td>{OnHand}</td>");
sHtml.Append("</tr> <tpl if=\"[xcount-xindex]==0\">");
sHtml.Append(" </table> </tpl> </tpl>");
cmb.Template.Html = sHtml.ToString();
Panel1.Items.Add(cmb);
如果您未绑定商店,则ComboBox将显示在页面上。 如果存储已绑定,则不会显示任何内容。浏览器会显示错误消息。 enter image description here 如何解决这个问题?
答案 0 :(得分:0)
HttpProxy proxy = new HttpProxy
{
Method = HttpMethod.POST,
Url = "../../../Handlers/BoneWL.ashx"
};
// Create Reader
Ext.Net.JsonReader reader = new Ext.Net.JsonReader
{
Root = "plants",
TotalProperty = "total",
Fields = {
new RecordField("ItemCode"),
new RecordField("ItemName"),
new RecordField("OnHand")
}
};
// Add Proxy and Reader to Store
Store store = new Store
{
Proxy = { proxy },
Reader = { reader },
AutoLoad = false
};
// Create ComboBox
Ext.Net.ComboBox cmb = new Ext.Net.ComboBox
{
DisplayField = "ItemCode",
ValueField = "ItemCode",
TypeAhead = false,
LoadingText = "加载中...",
Width = 240,
PageSize = 10,
HideTrigger = true,
ItemSelector = "tr.list-item",
MinChars = 1,
Store = { store }
};
cmb.Listeners.TriggerClick.Handler = "UseDirectEvents('1');WinRowCancelEdit();";
cmb.Triggers.Add(new FieldTrigger() { Icon = TriggerIcon.Search });
cmb.TriggerIcon = TriggerIcon.Search;
StringBuilder sHtml = new StringBuilder();
sHtml.Append(" <tpl for=\".\"><tpl if=\"[xindex] == 1\">");
sHtml.Append("<table class=\"cbStates-list\" ><tr>");
sHtml.Append("<th style=\"color: #2f353b !important;\">ItemCode</th>");
sHtml.Append(" <th style=\"color: #2f353b !important;\">ItemName</th>");
sHtml.Append("<th style=\"color: #2f353b !important;\">OnHand</th>");
sHtml.Append("</tr> </tpl>");
sHtml.Append("<tr class=\"list-item\">");
sHtml.Append("<td style=\"padding:3px 0px;\">{ItemCode}</td>");
sHtml.Append("<td>{ItemName}</td>");
sHtml.Append("<td>{OnHand}</td>");
sHtml.Append("</tr> <tpl if=\"[xcount-xindex]==0\">");
sHtml.Append(" </table> </tpl> </tpl>");
cmb.Template.Html = sHtml.ToString();
Panel1.Items.Add(cmb);