因为Easy UI 是PHP语言,所以自己尝试修改
以下是C# 前后台的配合
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"/>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
//$('#tb').textbox('readonly', true);
function newUser() {
$('#dlg').dialog('open').dialog('setTitle', 'New User');
$('#fm').form('clear');
url = 'Controller.ashx?method=add';
$('input[name=id]').removeAttr("readonly");
}
function editUser() {
var row = $('#dg').datagrid('getSelected');
if (row) {
$('#dlg').dialog('open').dialog('setTitle', 'Edit User');
$('input[name=id]').attr('readonly', true);
$('#fm').form('load', row);
url = 'Controller.ashx?method=edit';
}
}
function saveUser() {
$('#fm').form('submit', {
url: url,
onSubmit: function () {
return $(this).form('validate');
},
success: function (result) {
var result = eval('(' + result + ')');
if (result.success) {
$.messager.show({
title: 'Success',
msg: result.success
});
} else {
$.messager.show({
title: 'Error',
msg: result.error
});
}
$('#dlg').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
});
}
function destroyUser() {
var row = $('#dg').datagrid('getSelected');
if (row) {
$.messager.confirm('Confirm', 'Are you sure you want to destroy this Book?', function (r) {
if (r) {
//$.post('Controller.ashx?method=delete', { id: row.id }, function (result) {
// //if (result.success) {
// // $('#dg').datagrid('reload'); // reload the user data
// // $.messager.show({ // show error message
// // title: 'Success',
// // msg: result.success
// // });
// //} else {
// // $.messager.show({ // show error message
// // title: 'Error',
// // msg: result.error
// // });
// //}
// alert(result);
//}, 'json');
$.ajax({
url: 'Controller.ashx?method=delete',
type: 'post',
data: { id: row.id },
// contentType: 'json',
dataType: 'json',
success: function (data) {
alert(data.success);
$('#dg').datagrid('reload');
},
error: function (data) {
alert(data.error);
}
});
}
});
}
}
</script>
</head>
<body>
<form action="Controller.ashx" method="post">
<!--<input type="text" name="name"/><br />
<input type="submit" value="提交"/>
<div id="p" class="easyui-panel" style="width:500px;height:200px;padding:10px;"
title="My Panel" iconCls="icon-save" collapsible="true">
The panel content
</div>
<div id="Div1" class="easyui-panel" style="width:500px;height:200px;padding:10px;"
title="My Panel" data-options="iconCls:'icon-save',collapsible:true">
The panel content
</div>
<input class="easyui-combobox" name="language"
data-options="
url:'combobox_data.json',
valueField:'id',
textField:'text',
panelHeight:'auto',
onSelect:function(record){
alert(record.text)
}"/>-->
<table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
url='Controller.ashx?method=query'
toolbar="#toolbar"
rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr>
<th field="id" width="50">id</th>
<th field="name" width="50">name</th>
<th field="url" width="50">url</th>
<th field="alexa" width="50">alexa</th>
</tr>
</thead>
</table>
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" οnclick="newUser()">New User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" οnclick="editUser()">Edit User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" οnclick="destroyUser()">Remove User</a>
</div>
</form>
<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
closed="true" buttons="#dlg-buttons">
<div class="ftitle">User Information</div>
<form id="fm" method="post">
<table>
<div class="fitem" id="fitem">
<tr>
<td> <label>id:</label></td>
<td><input name="id" class="easyui-validatebox" required="true"></td>
</tr>
</div>
<div class="fitem" >
<tr>
<td><label>name:</label></td>
<td><input name="name" class="easyui-validatebox" required="true"></td>
</tr>
</div>
<div class="fitem">
<tr>
<td><label>url:</label></td>
<td><input name="url"></td>
</tr>
</div>
<div class="fitem">
<tr>
<td><label>alexa:</label></td>
<td><input name="alexa"></td>
</tr>
</div>
<!--<div class="fitem">
<label>Email:</label>
<input name="email" class="easyui-validatebox" validType="email">
</div>-->
</table>
</form>
</div>
<div id="dlg-buttons">
<a href="#" class="easyui-linkbutton" iconCls="icon-ok" οnclick="saveUser()">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" οnclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>
</body>
</html>
public class Controller : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
String method = context.Request.Params.Get("method");
if (method == "query")
{
getData(context);
}
else if (method == "add")
{
String id = context.Request.Form["id"].ToString();
String name = context.Request.Form["name"].ToString();
String url = context.Request.Form["url"].ToString();
String alexa = context.Request.Form["alexa"].ToString();
String sql = "insert into websites values('" + id + "','" + name + "','" + url + "','" + alexa + "')";
addData(context, sql);
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
context.Response.Write(jsSerializer.Serialize("添加成功"));
context.Response.End();
}
else if (method == "edit")
{
String id = context.Request.Form["id"].ToString();
String name = context.Request.Form["name"].ToString();
String url = context.Request.Form["url"].ToString();
String alexa = context.Request.Form["alexa"].ToString();
String sql = "update websites set name='" + name + "',url='" + url + "', alexa='" + alexa + "' where id='" + id + "'";
editData(context, sql);
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
Dictionary<String, String> map = new Dictionary<string, string>();
map.Add("success","成功");
map.Add("error", "失败");
context.Response.Write(jsSerializer.Serialize(map));
context.Response.End();
}
else if (method == "delete")
{
Dictionary<String, String> map = new Dictionary<string, string>();
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
try
{
String id = context.Request.Form["id"].ToString();
String sql = "delete from websites where id=" + id;
connectDB cd = new connectDB();
cd.executeNoQuery(sql);
map.Add("success", "成功");
context.Response.Write(jsSerializer.Serialize(map));
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception e)
{
map.Add("error", e.Message.ToString());
context.Response.Write(jsSerializer.Serialize(map));
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
public void addData(HttpContext context,String sql)
{
connectDB cd = new connectDB();
cd.executeNoQuery(sql);
}
public void editData(HttpContext context, String sql)
{
connectDB cd = new connectDB();
cd.executeNoQuery(sql);
}
public void getData(HttpContext context)
{
connectDB cd = new connectDB();
DataTable dt = cd.executeDataTable("select id,name,url,alexa from websites");
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
for (int i = 0; i < dt.Rows.Count; i++)
{
Dictionary<string, string> d = new Dictionary<string, string>();
for(int j=0;j<dt.Columns.Count;j++)
{
d.Add(dt.Columns[j].ColumnName,dt.Rows[i][j].ToString());
}
list.Add(d);
}
// string commentsJson = DataTableToJsonWithJavaScriptSerializer(dt);
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string commentsJson = jsSerializer.Serialize(list);
context.Response.Write(commentsJson);
// context.Response.End();
}
public string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
public void Alert(string message, HttpContext context)
{
string js = @"<Script language='JavaScript'>
alert('" + message + "');</Script>";
context.Response.Write(js);
}
public bool IsReusable
{
get
{
return false;
}
}
}