一、生成表格 GridView
创建GridView有两种方式:
1.以编程方式创建,指定datagrid属性即可(其它属性可查看demo或文档)
$('#tableView').datagrid({
url: '../../Controller/RoleController.ashx?OperationType=role&page=1&rows=' + AdjustPageSize()
});
2.在HTML标记上指定CSS class类,EasyUI会通过类名easyui-datagrid作隐式创建
<table id="tableView" class="easyui-datagrid" singleselect="true" title="GridView"
idfield="RoleId" width="100%" height="auto" nowrap="false" striped="true" rownumbers="true"
pagination="true">
</table>
其二者不可重叠,否则会产生两次异步请求。
二、导出Excel,解决乱码
string filename = "角色信息.xls";
filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
context.Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
context.Response.Clear();
三、ashx文件中使用Session
1、using System.Web.SessionState;
2、类实现IRequiresSessionState
public class DoAjax : IHttpHandler,IRequiresSessionState
{
}
完成以上两步就可以正常使用Session了。
四、ashx文件中使用Server.MapPath()
HttpContext.Current.Server.MapPath("")
五、单数据库 事务处理(非TransactionScope)
如果使用TransactionScope,会导致分布式事务被触发,见倒数第三位所述(来自stackoverflow.com)
而一个针对单数据库的建议可以查看这里(来自stackoverflow.com)
我自己的实现跟其建议差不多,且是有效的:
try { if (dbContext.Connection.State == System.Data.ConnectionState.Closed) { dbContext.Connection.Open(); } /* 为了兼顾那些非EF操作,即采用直接拼Sql作数据库访问时,数据库链接需作一点转换: cmd.Connection = (dbContext.Connection.BeginTransaction as EntityConnection).StoreConnection; * */ using (DbTransaction ts = dbContext.Connection.BeginTransaction()) { BaseRole _role = role; BaseSequnce sequnce = dbContext.BaseSequnces.SingleOrDefault(s => s.SequnceId == 10000011); sequnce.KeySequnce = sequnce.KeySequnce + 1; sequnce.SortReduction = sequnce.SortReduction - 1; dbContext.SaveChanges(); sequnce = dbContext.BaseSequnces.SingleOrDefault(s => s.SequnceId == 10000011); _role.RoleId = sequnce.KeySequnce; _role.SortCode = sequnce.SortReduction; //最后一步 dbContext.BaseRoles.AddObject(_role); dbContext.SaveChanges(); ts.Commit(); } return true; } catch { return false; } finally { if (dbContext.Connection.State == System.Data.ConnectionState.Open) { dbContext.Connection.Close(); } }