Extjs三种取数据方法
页面文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ComboPage.aspx.cs" Inherits="ComboPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title><link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css">
<script type="text/javascript" src="ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function () {
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
//从aspx文件中取数据
var dsaspx = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: "re.aspx"
}),
reader: new Ext.data.JsonReader({
root: 'topics',
totalProperty: 'totalCount',
id: 'id'
}, [
{ name: 'id', mapping: 'id' },
{ name: 'value', mapping: 'value' }
])
});
dsaspx.load();
var search = new Ext.form.ComboBox({
store: dsaspx,
displayField: 'value',
typeAhead: true,
mode: 'local',
forceSelection: true,
triggerAction: 'all',
emptyText: 'Select a state...',
selectOnFocus: true,
applyTo: 'search'
});
//从aspx文件中取数据
//从webservice中取数据ajax
Ext.Ajax.request({
url: 'WebService.asmx/GetComboxFirst',
method: 'POST',
//jsonData: { UserName: "" },
headers: { 'Content-Type': 'application/json;utf-8' },
success: General,
failure: On_Error
});
function General(result) {
var data = Ext.util.JSON.decode(result.responseText);
var ds = new Ext.data.Store({
proxy: new Ext.data.MemoryProxy(data.d),
reader: new Ext.data.JsonReader({ fields: [{ name: 'strText', type: 'string' }, { name: 'strValue', type: 'string'}] })
});
ds.load();
var combox1 = new Ext.form.ComboBox({
store: ds,
displayField: 'strText',
typeAhead: true,
mode: 'local',
forceSelection: true,
triggerAction: 'all',
emptyText: 'Select a state...',
selectOnFocus: true,
applyTo: 'combox1'
});
};
function On_Error() {
};
//从webservice中取数据ajax
//从webserivce中取通过HttpProxy,因为asp.net数据是放在.d里面的,所以需要重写JsonReader来取数据.
bJsonReader = Ext.extend(Ext.data.JsonReader, {
read: function (response) {
var json = response.responseText;
alert(json);
var o = Ext.decode(json);
alert(o.d);
this.responseText = json;
if (!o) {
throw { message: 'JsonReader.read: Json object not found' };
}
return this.readRecords(o.d);
}
});
var dshttpProxy = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: "WebService.asmx/GetComboxFirst", method: 'POST',headers: { 'Content-Type': 'application/json;utf-8' }
}),
reader: new bJsonReader({
fields: [{ name: 'strText', type: 'string' }, { name: 'strValue', type: 'string'}]
})
});
dshttpProxy.load();
var combox2 = new Ext.form.ComboBox({
store: dshttpProxy,
displayField: 'strText',
typeAhead: true,
mode: 'local',
forceSelection: true,
triggerAction: 'all',
emptyText: 'Select a state...',
selectOnFocus: true,
applyTo: 'combox2'
});
//从webserivce中取通过HttpProxy
})
</script>
</head>
<body>
<input type="text" id="search" />
<input type="text" id="combox1" />
<input type="text" id="combox2" />
</body>
</html>
Webservice
[ScriptService]
[WebMethod]
public List<Combox_InfoFirst> GetComboxFirst()
{
List<Combox_InfoFirst> list = new List<Combox_InfoFirst>();
for (int i = 0; i < 10; i++)
{
Combox_InfoFirst info = new Combox_InfoFirst();
info.strText = "Text" + i.ToString();
info.strValue = "Value" + i.ToString();
list.Add(info);
}
return list;
}
public class Combox_InfoFirst
{
private string _strText = "";
private string _strValue = "";
public string strText
{
set { _strText = value; }
get { return _strText; }
}
public string strValue
{
set { _strValue = value; }
get { return _strValue; }
}
}