using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
Service;
using
System.Web.Services;
namespace
ExtJs_ASP_Insert_Delete_Modify_Update
{
///
///
$codebehindclassname$ 的摘要说明
///
[WebService(Namespace
=
"
http://tempuri.org/
"
)]
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
public
class
Handler1 : IHttpHandler
{
private
DataClasses1DataContext db
=
new
DataClasses1DataContext();
public
void
ProcessRequest(HttpContext context)
{
string
tem_response
=
string
.Empty;
string
tem_str
=
context.Request.QueryString[
"
parm
"
];
if
(
string
.IsNullOrEmpty(tem_str))
{
context.Response.Write(
"
error!
"
);
return
;
}
context.Response.ContentType
=
"
text/json
"
;
if
(tem_str
==
"
Get
"
)
{
context.Response.Write(Get(context));
}
else
if
(tem_str
==
"
Insert
"
)
{
context.Response.Write(Insert(context));
}
else
if
(tem_str
==
"
Update
"
)
{
context.Response.Write(Update(context));
}
else
if
(tem_str
==
"
Delete
"
)
{
context.Response.Write(Delete(context));
}
return
;
}
public
string
Get(HttpContext context)
{
int
tem_start
=
int
.Parse(context.Request.Form[
"
start
"
]
+
""
);
//
分页需要limit,start是mysql里用的(或取当页开始的记录标识编号)
int
tem_limit
=
int
.Parse(context.Request.Form[
"
limit
"
]
+
""
);
//
或取每页记录数
var tem_query
=
from a
in
db.tb_user
from b
in
db.tb_score
where
a.use_id
==
b.use_id
orderby a.use_id descending
select
new
{ a.use_id, a.use_name, a.use_sex, a.use_address, b.sco_subject, b.sco_score };
int
tem_Count
=
tem_query.Count();
//
所要记录数
int
tem_PageNum
=
tem_start
/
tem_limit;
//
共有页数
int
tem_PageSize
=
tem_limit;
tem_query
=
tem_query.Skip(tem_PageSize
*
tem_PageNum).Take(tem_PageSize);
//
当前页记录
string
JsonSource
=
JsonHelper.Jso_ToJSON(tem_query);
//
当前页记录转成JSON格式
string
strJsonSource
=
@"
{""totalCount"":""
"
+
tem_Count
+
""
;
strJsonSource
=
strJsonSource
+
@"
"",""data"":
"
+
JsonSource
+
"
}
"
;
//
Grid的分页区显示所有记录数增加totalCount信息
return
strJsonSource;
}
public
string
Insert(HttpContext context)
{
string
tem_out_message
=
null
;
try
{
db.pro_Insert_values(
int
.Parse(context.Request.QueryString[
"
use_id
"
]), context.Request.QueryString[
"
use_name
"
].ToString(), context.Request.QueryString[
"
use_sex
"
].ToString(),
context.Request.QueryString[
"
use_address
"
].ToString(), context.Request.QueryString[
"
sco_subject
"
].ToString(),
short
.Parse(context.Request.QueryString[
"
sco_score
"
]),
ref
tem_out_message);
}
catch
{
tem_out_message
=
"
-1
"
;
}
if
(tem_out_message
==
"
1
"
)
{
return
"
{success:true}
"
;
}
else
if
(tem_out_message
==
"
-1
"
)
{
return
"
{ success: false, errors:{info: '输入类型不匹配'} }
"
;
}
else
{
return
"
{ success: false, errors:{info: '插入失败'} }
"
;
}
}
public
string
Update(HttpContext context)
{
string
tem_out_message
=
null
;
try
{
db.pro_Update_values(
int
.Parse(context.Request.QueryString[
"
use_id
"
]), context.Request.QueryString[
"
use_name
"
].ToString(), context.Request.QueryString[
"
use_sex
"
].ToString(),
context.Request.QueryString[
"
use_address
"
].ToString(), context.Request.QueryString[
"
sco_subject
"
].ToString(),
short
.Parse(context.Request.QueryString[
"
sco_score
"
]),
ref
tem_out_message);
}
catch
{
tem_out_message
=
"
-1
"
;
}
if
(tem_out_message
==
"
1
"
)
{
return
"
{success:true}
"
;
}
else
if
(tem_out_message
==
"
-1
"
)
{
return
"
{ success: false, errors:{info: '输入类型不匹配'} }
"
;
}
else
{
return
"
{ success: false, errors:{info: '更新失败'} }
"
;
}
}
public
string
Delete(HttpContext context)
{
string
tem_out_message
=
null
;
db.pro_Delete_values(
int
.Parse(context.Request.Form[
"
use_id
"
]),
ref
tem_out_message);
if
(tem_out_message
==
"
1
"
)
{
return
"
{success:true}
"
;
}
else
{
return
"
{ success: false, errors:{info: '删除失败'} }
"
;
}
}
public
bool
IsReusable
{
get
{
return
false
;
}
}
}
}
var
_start
=
0
var
_limit
=
5
/*
********
*/
/*
增加
*/
/*
*******
*/
var
formpanel_add
=
new
Ext.FormPanel({
labelWidth:
75
,
bodyStyle:
'
padding: 10px
'
,
baseCls:
'
x-plain
'
,
defauls: { width:
200
},
defaultType:
'
textfield
'
,
autoHeight:
true
,
items: [
{
fieldLabel:
'
用户帐户
'
,
name:
'
use_id
'
,
blankText:
'
用户帐户不能为空!
'
,
allowBlank:
false
,
regex:
/
^[1-9]\d*|0$
/
,
regexText:
'
必须为有效的整数!
'
}, {
fieldLabel:
'
用户姓名
'
,
name:
'
use_name
'
,
blankText:
'
用户姓名不能为空!
'
,
maxLength:
'
10
'
,
maxLengthText:
'
最大不超过10个字符
'
,
allowBlank:
false
}, {
xtype:
"
panel
"
,
layout:
"
column
"
,
baseCls:
'
x-plain
'
,
fieldLabel:
'
用户性别
'
,
isFormField:
true
,
items: [{
columnWidth: .
5
,
xtype:
"
radio
"
,
boxLabel:
"
男
"
,
name:
"
use_sex
"
,
inputValue:
"
男
"
,
checked:
true
,
id:
"
boy
"
}, {
columnWidth: .
5
,
xtype:
"
radio
"
,
boxLabel:
"
女
"
,
name:
"
use_sex
"
,
inputValue:
"
女
"
,
id:
"
girl
"
}]
}, {
fieldLabel:
'
用户地址
'
,
name:
'
use_address
'
,
blankText:
'
用户地址不能为空!
'
,
maxLength:
'
25
'
,
maxLength:
'
最大不超过25个字符!
'
,
allowBlank:
false
}, {
fieldLabel:
'
考试科目
'
,
name:
'
sco_subject
'
,
blankText:
'
考试科目不能为空!
'
,
maxLength:
'
25
'
,
maxLength:
'
最大不超过25个字符!
'
,
allowBlank:
false
}, {
fieldLabel:
'
考试分数
'
,
name:
'
sco_score
'
,
blankText:
'
考试分数不能为空!
'
,
allowBlank:
false
,
regex:
/
^[1-9]\d*|0$
/
,
regexText:
'
必须为有效的整数!
'
}
]
});
var
window_add
=
new
Ext.Window({
title:
'
学生各科成绩增加
'
,
modal:
true
,
id:
'
1
'
,
width:
300
,
height:
300
,
resizable:
false
,
plain:
true
,
iconCls:
'
add
'
,
bodyStyle:
'
padding:20px;
'
,
buttonAlign:
'
center
'
,
closeAction:
'
hide
'
,
items: formpanel_add,
listeners: {
"
show
"
:
function
() {
formpanel_add.getForm().reset();
}
},
buttons: [{
text:
'
保存
'
,
iconCls:
'
page_save
'
,
handler: submit
}, {
text:
'
重置
'
,
iconCls:
'
arrow_undo
'
,
handler:
function
() {
formpanel_add.getForm().reset();
}
}]
});
var
TopicRecord
=
Ext.data.Record.create([
{ name:
'
use_id
'
, mapping:
'
use_id
'
},
{ name:
'
use_name
'
, mapping:
'
use_name
'
},
{ name:
'
use_sex
'
},
{ name:
'
use_address
'
, mapping:
'
use_address
'
},
{ name:
'
sco_subject
'
, mapping:
'
sco_subject
'
},
{ name:
'
sco_score
'
, mapping:
'
sco_score
'
}
]);
function
submit() {
if
(
!
formpanel_add.getForm().isValid())
return
;
formpanel_add.getForm().submit({
waitMsg:
'
正在提交数据
'
,
waitTitle:
'
提示
'
,
url:
'
Handler1.ashx?parm=Insert
'
,
method:
'
get
'
,
success:
function
(form, action) {
var
sex
=
"
男
"
;
if
(Ext.getCmp(
"
girl
"
).checked
==
true
)
sex
=
"
女
"
;
var
p
=
new
TopicRecord({
//
创建一个记录源
use_id: formpanel_add.getForm().findField(
'
use_id
'
).getValue(),
use_name: formpanel_add.getForm().findField(
'
use_name
'
).getValue(),
use_sex: sex,
use_address: formpanel_add.getForm().findField(
'
use_address
'
).getValue(),
sco_subject: formpanel_add.getForm().findField(
'
sco_subject
'
).getValue(),
sco_score: formpanel_add.getForm().findField(
'
sco_score
'
).getValue()
});
store.insert(
0
, p);
Ext.Msg.alert(
'
提示
'
,
'
保存成功
'
);
},
failure:
function
(form, action) {
Ext.Msg.alert(
'
错误
'
,
'
失败:
'
+
action.result.errors.info
+
'
'
);
}
});
}
/*
********
*/
/*
更新
*/
/*
*******
*/
var
formpanel_page_edit
=
new
Ext.FormPanel({
labelWidth:
75
,
bodyStyle:
'
padding: 10px
'
,
baseCls:
'
x-plain
'
,
defauls: { width:
200
},
defaultType:
'
textfield
'
,
autoHeight:
true
,
items: [
{
fieldLabel:
'
用户帐户
'
,
name:
'
use_id
'
,
blankText:
'
用户帐户不能为空!
'
,
allowBlank:
false
,
regex:
/
^[1-9]\d*|0$
/
,
regexText:
'
必须为有效的整数!
'
,
readOnly:
true
}, {
fieldLabel:
'
用户姓名
'
,
name:
'
use_name
'
,
blankText:
'
用户姓名不能为空!
'
,
maxLength:
'
10
'
,
maxLengthText:
'
最大不超过10个字符!
'
,
allowBlank:
false
}, {
xtype:
"
panel
"
,
layout:
"
column
"
,
baseCls:
'
x-plain
'
,
fieldLabel:
'
用户性别
'
,
isFormField:
true
,
items: [{
columnWidth: .
5
,
xtype:
"
radio
"
,
boxLabel:
"
男
"
,
name:
"
use_sex
"
,
inputValue:
"
男
"
,
id:
"
_boy
"
}, {
columnWidth: .
5
,
xtype:
"
radio
"
,
boxLabel:
"
女
"
,
name:
"
use_sex
"
,
inputValue:
"
女
"
,
id:
"
_girl
"
}]
}, {
fieldLabel:
'
用户地址
'
,
name:
'
use_address
'
,
blankText:
'
用户地址不能为空!
'
,
maxLength:
'
25
'
,
maxLengthText:
'
最大不超过25个字符!
'
,
allowBlank:
false
}, {
fieldLabel:
'
考试科目
'
,
name:
'
sco_subject
'
,
blankText:
'
考试科目不能为空!
'
,
maxLength:
'
25
'
,
maxLengthText:
'
最大不超过25个字符!
'
,
allowBlank:
false
}, {
fieldLabel:
'
考试分数
'
,
name:
'
sco_score
'
,
blankText:
'
考试分数不能为空!
'
,
allowBlank:
false
,
regex:
/
^[1-9]\d*|0$
/
,
regexText:
'
必须为有效的整数!
'
}
]
})
var
window_page_edit
=
new
Ext.Window({
title:
'
学生各科成绩更新
'
,
modal:
true
,
width:
300
,
height:
300
,
resizable:
false
,
plain:
true
,
iconCls:
'
page_edit
'
,
bodyStyle:
'
padding:20px;
'
,
buttonAlign:
'
center
'
,
closeAction:
'
hide
'
,
items: formpanel_page_edit,
listeners: {
"
show
"
:
function
() {
row
=
Ext.getCmp(
"
grid
"
).getSelectionModel().getSelections();
formpanel_page_edit.getForm().reset();
formpanel_page_edit.getForm().loadRecord(row[
0
]);
}
},
buttons: [{
text:
'
保存
'
,
iconCls:
'
page_save
'
,
handler: Update
}, {
text:
'
重置
'
,
iconCls:
'
arrow_undo
'
,
handler:
function
() {
formpanel_page_edit.getForm().loadRecord(row[
0
]);
}
}]
});
function
Update() {
if
(
!
formpanel_page_edit.getForm().isValid())
return
;
formpanel_page_edit.getForm().submit({
waitMsg:
'
正在提交数据
'
,
waitTitle:
'
提示
'
,
url:
'
Handler1.ashx?parm=Update
'
,
method:
'
get
'
,
success:
function
(form, action) {
var
row_number
=
Ext.getCmp(
"
grid
"
).getSelectionModel().last;
var
row
=
Ext.getCmp(
"
grid
"
).getSelectionModel().getSelections();
var
sex
=
"
男
"
;
if
(Ext.getCmp(
"
_girl
"
).checked
==
true
)
sex
=
"
女
"
;
var
p
=
new
TopicRecord({
//
创建一个记录源
use_id: formpanel_page_edit.getForm().findField(
'
use_id
'
).getValue(),
use_name: formpanel_page_edit.getForm().findField(
'
use_name
'
).getValue(),
use_sex: sex,
use_address: formpanel_page_edit.getForm().findField(
'
use_address
'
).getValue(),
sco_subject: formpanel_page_edit.getForm().findField(
'
sco_subject
'
).getValue(),
sco_score: formpanel_page_edit.getForm().findField(
'
sco_score
'
).getValue()
});
store.remove(row);
store.insert(row_number, p);
Ext.getCmp(
"
grid
"
).getSelectionModel().selectRow(row_number);
Ext.Msg.alert(
'
提示
'
,
'
保存成功
'
);
},
failure:
function
(form, action) {
Ext.Msg.alert(
'
错误
'
,
'
失败:
'
+
action.result.errors.info
+
'
'
);
}
});
}
/*
********
*/
/*
删除
*/
/*
*******
*/
function
showResult(btn) {
if
(btn
==
'
yes
'
) {
Ext.Msg.wait(
"
请等候
"
,
"
删除中
"
,
"
操作进行中......
"
);
var
row
=
Ext.getCmp(
"
grid
"
).getSelectionModel().getSelections();
var
jsonData
=
""
;
for
(
var
i
=
0
, len
=
row.length; i
<
len; i
++
) {
var
ss
=
row[i].get(
"
use_id
"
);
if
(i
==
0
)
jsonData
=
jsonData
+
ss;
//
这样处理是为了删除的Lambda语句方便
else
jsonData
=
jsonData
+
"
,
"
+
ss;
//
这样处理是为了删除的Lambda语句方便
}
//
alert(jsonData);
var
conn
=
new
Ext.data.Connection();
conn.request({
url:
"
Handler1.ashx?parm=Delete
"
,
//
请注意引用的路径
params: { use_id: jsonData },
method:
'
post
'
,
scope:
this
,
callback:
function
(options, success, response) {
if
(success) {
Ext.MessageBox.alert(
"
提示
"
,
"
所选记录成功删除!
"
);
var
row
=
Ext.getCmp(
"
grid
"
).getSelectionModel().getSelections();
store.remove(row);
}
else
{ Ext.MessageBox.alert(
"
提示
"
,
"
所选记录删除失败!
"
); }
}
})
}
};
Ext.onReady(
function
() {
Ext.QuickTips.init();
store
=
new
Ext.data.JsonStore({
root:
'
data
'
,
totalProperty:
'
totalCount
'
,
url:
'
Handler1.ashx?parm=Get
'
,
fields: [
{ name:
'
use_id
'
, type:
'
int
'
},
{ name:
'
use_name
'
},
{ name:
'
use_sex
'
},
{ name:
'
use_address
'
},
{ name:
'
sco_subject
'
},
{ name:
'
sco_score
'
}
]
});
var
grid
=
new
Ext.grid.GridPanel({
frame:
true
,
title:
'
学生各科成绩表
'
,
stripeRows:
true
,
//
斑马线
store: store,
id:
'
grid
'
,
applyTo:
'
app_grid
'
,
trackMouseOver:
true
,
height:
300
,
width:
500
,
loadMask: { msg:
'
正在加载数据,请稍侯……
'
},
viewConfig: {
forceFit:
true
},
columns: [
new
Ext.grid.RowNumberer(),
//
行号
new
Ext.grid.CheckboxSelectionModel({ singleSelect:
true
}),
{ header:
'
用户帐户
'
, dataIndex:
'
use_id
'
, sortable:
true
},
{ header:
'
用户姓名
'
, dataIndex:
'
use_name
'
, sortable:
true
},
{ header:
'
用户性别
'
, dataIndex:
'
use_sex
'
, sortable:
true
},
{ header:
'
用户地址
'
, dataIndex:
'
use_address
'
, sortable:
true
},
{ header:
'
考试科目
'
, dataIndex:
'
sco_subject
'
, sortable:
true
},
{ header:
'
考试分数
'
, dataIndex:
'
sco_score
'
, sortable:
true
}
],
tbar:
new
Ext.Toolbar([
'
-
'
,
{
text:
'
增加数据
'
,
iconCls:
'
add
'
,
handler:
function
() {
window_add.show();
}
},
'
-
'
,
{
text:
'
删除数据
'
,
iconCls:
'
delete
'
,
handler:
function
() {
var
s
=
grid.getSelectionModel().getSelections();
if
(s.length
==
0
) {
//
判断有没有选中行
Ext.Msg.alert(
'
提示
'
,
'
你还没有选择要操作的记录!
'
);
}
else
if
(s.length
>
1
) {
Ext.Msg.alert(
'
提示
'
,
'
不能删除多个操作记录!
'
);
}
else
{
Ext.MessageBox.confirm(
'
提示
'
,
'
您确认要删除当前记录吗?
'
, showResult);
};
}
},
'
-
'
,
{
text:
'
更新数据
'
,
iconCls:
'
page_edit
'
,
handler:
function
() {
var
s
=
grid.getSelectionModel().getSelections();
if
(s.length
==
0
) {
//
判断有没有选中行
Ext.Msg.alert(
'
提示
'
,
'
你还没有选择要操作的记录!
'
);
}
else
if
(s.length
>
1
) {
Ext.Msg.alert(
'
提示
'
,
'
不能更新多个操作记录!
'
);
}
else
{
window_page_edit.show();
};
}
},
'
-
'
]),
bbar:
new
Ext.PagingToolbar({
//
分页
pageSize: _limit,
store: store,
displayInfo:
true
,
//
非要为true,不然不会显示下面的分页按钮
displayMsg:
'
第 {0} 条到 {1} 条,一共 {2} 条记录
'
,
emptyMsg:
"
没有记录
"
})
})
store.load({ params: { start: _start, limit: _limit} });
})