FineUI 官方论坛
标题:
FINEUI与EF的对接
[打印本页]
作者:
leetle
时间:
2013-10-22 11:04
标题:
FINEUI与EF的对接
本帖最后由 leetle 于 2013-10-22 11:13 编辑
QQ 909994561
Email
henanluheng@126.com
#region Protected Method
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void trDept_NodeExpand(object sender, TreeExpandEventArgs e)
{
LoadChildNodes(e.Node);
}
/// <summary>
/// 页面初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetPermissionButtons(Toolbar1);
SetPermissionGrid(Grid1);
//加载部门树
LoadTreeSource();
btnAdd.OnClientClick = Window1.GetShowReference(string.Format("./account_edit.aspx?action=1&orgid={0}",
trDept.SelectedNodeID), "添加用户");
}
}
/// <summary>
/// 部门树加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void trDept_NodeCommand(object sender, TreeCommandEventArgs e)
{
BindDataGrid();
btnAdd.OnClientClick = Window1.GetShowReference(string.Format("./account_edit.aspx?action=1&orgid={0}",
trDept.SelectedNodeID), "添加用户");
}
/// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
Grid1.PageIndex = e.NewPageIndex;
BindDataGrid();
}
作者:
leetle
时间:
2013-10-22 11:04
/// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
BindDataGrid();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort(object sender, GridSortEventArgs e)
{
SortField = string.Format(@"{0}", e.SortField);
SortDirection = e.SortDirection;
BindDataGrid();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnBatchDelete_Click(object sender, EventArgs e)
{
IEnumerable<int> selectIds = GetSelectIds();
try
{
Log(string.Format(@"删除帐号记录ID:{0}成功。", selectIds));
AccountService.Update(p => selectIds.Contains(p.id), p => new base_account {deleteflag = 1});
Alert.Show("删除成功!", MessageBoxIcon.Information);
BindDataGrid();
}
catch (Exception)
{
Alert.Show("删除失败!", MessageBoxIcon.Warning);
}
}
//protected void btnDelete_Click(object sender, EventArgs e)
//{
// try
// {
// if (Grid1.SelectedRowIndexArray.Length == 0)
// {
// Alert.Show("请至少选择一项!", MessageBoxIcon.Information);
// }
// else if (Grid1.SelectedRowIndexArray.Length > 1)
// {
// Alert.Show("只能选择一项!", MessageBoxIcon.Information);
// }
// else
// {
// int sid = Convert.ToInt32(Grid1.DataKeys[Grid1.SelectedRowIndexArray[0]][0]);
// AccountService.Update(p => p.id == sid, p => new base_account {deleteflag = 1});
// Log(string.Format(@"删除帐号记录ID:{0}成功。", sid));
// Alert.Show("删除成功!", MessageBoxIcon.Information);
// BindDataGrid();
// }
// }
// catch (Exception)
// {
// Alert.Show("删除失败!", MessageBoxIcon.Warning);
// }
//}
/// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEdit_Click(object sender, EventArgs e)
{
try
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.Show("请至少选择一项!", MessageBoxIcon.Information);
}
else if (Grid1.SelectedRowIndexArray.Length > 1)
{
Alert.Show("只能选择一项!", MessageBoxIcon.Information);
}
else
{
int sid = Convert.ToInt32(Grid1.DataKeys[Grid1.SelectedRowIndexArray[0]][0]);
PageContext.RegisterStartupScript(
Window1.GetShowReference(string.Format("./account_edit.aspx?id={0}&orgid={1}&action=2",
sid, trDept.SelectedNodeID), "编辑用户"));
}
}
catch (Exception)
{
Alert.Show("删除失败!", MessageBoxIcon.Warning);
}
}
/// <summary>
/// 禁用/启用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEnabled_Click(object sender, EventArgs e)
{
try
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.Show("请至少选择一项!", MessageBoxIcon.Information);
}
else if (Grid1.SelectedRowIndexArray.Length > 1)
{
Alert.Show("只能选择一项!", MessageBoxIcon.Information);
}
else
{
int sid = Convert.ToInt32(Grid1.DataKeys[Grid1.SelectedRowIndexArray[0]][0]);
base_account account = AccountService.FirstOrDefault(p => p.id == sid);
account.account_flag = account.account_flag == 1 ? 0 : 1;
if (AccountService.SaveChanges() > 0)
{
BindDataGrid();
Alert.Show("提交成功。", MessageBoxIcon.Information);
//记录日志
Log(string.Format(@"禁用帐号{0}成功。", account.account_number));
}
else
{
Alert.Show("提交失败,请重试!", MessageBoxIcon.Error);
}
}
}
catch (Exception)
{
Alert.Show("删除失败!", MessageBoxIcon.Warning);
}
}
/// <summary>
/// 初始化密码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnInitPassword_Click(object sender, EventArgs e)
{
IEnumerable<int> selectIds = GetSelectIds();
try
{
AccountService.Update(p => selectIds.Contains(p.id),
p => new base_account {account_password = LHEncrypt.Encrypt("123456")});
Alert.Show("密码初始化成功!", MessageBoxIcon.Information);
BindDataGrid();
}
catch (Exception)
{
Alert.Show("密码初始化失败!", MessageBoxIcon.Warning);
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
BindDataGrid();
}
/// <summary>
/// 导出本页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExport_Click(object sender, EventArgs e)
{
int output;
dynamic orderingSelector;
Expression<Func<base_account, bool>> predicate = BuildPredicate(out orderingSelector);
//取数据源
IQueryable<base_account> list = AccountService.Where(predicate, Grid1.PageSize, Grid1.PageIndex + 1,
orderingSelector, EnumHelper.ParseEnumByString<OrderingOrders>(SortDirection), out output);
ExportData(list);
}
作者:
leetle
时间:
2013-10-22 11:05
/// <summary>
/// 导出全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExportAll_Click(object sender, EventArgs e)
{
dynamic orderingSelector;
Expression<Func<base_account, bool>> predicate = BuildPredicate(out orderingSelector);
//取数据源
IQueryable<base_account> list = AccountService.Where(predicate,
orderingSelector, EnumHelper.ParseEnumByString<OrderingOrders>(SortDirection));
ExportData(list);
}
/// <summary>
/// 将数据转换为导出数据并导出到Excel
/// </summary>
/// <param name="queryData"></param>
private void ExportData(IQueryable<base_account> queryData)
{
var exportData = from q in queryData
select new BaseAccountExport
{
姓名 = q.account_name,
登录帐号 = q.account_number,
部门=q.account_org_name,
角色=q.account_role_name,
创建时间=(DateTime) q.createdon,
最后一次登录时间 = (DateTime) q.account_last_date,
性别=q.account_sex,
是否禁用=(int) q.account_flag
};
IList<BaseAccountExport> exportList = exportData.ToList();
if (exportList.Count > 0)
{
ReportExport.ListToExcel(exportList, Context.Response, HttpUtility.UrlEncode("系统帐号信息" + DateTime.Now.ToString("yyyyMMddHHmmss")));
Alert.Show("导出成功!", MessageBoxIcon.Information);
Context.Response.Flush();
Context.Response.End();
}
else
{
Alert.Show("没有要导出的数据列表!", MessageBoxIcon.Warning);
}
}
protected void Window1_Close(object sender, WindowCloseEventArgs e)
{
BindDataGrid();
}
#endregion
#region Private Method
/// <summary>
/// 树绑定
/// </summary>
private void LoadTreeSource()
{
trDept.Nodes.Clear();
base_company company = CompanyService.FirstOrDefault(p => p.deleteflag == 0);
var rootNode = new TreeNode
{
Text = company.com_name,
NodeID = company.id.ToString(CultureInfo.InvariantCulture),
EnablePostBack = true,
Expanded = true
};
trDept.Nodes.Add(rootNode);
//加载子部门信息
LoadChildNodes(rootNode);
//设置默认选择项
trDept.SelectedNodeID = rootNode.NodeID;
trDept_NodeCommand(null, null);
}
作者:
leetle
时间:
2013-10-22 11:05
/// <summary>
/// 绑定子节点
/// </summary>
/// <param name="node"></param>
private void LoadChildNodes(TreeNode node)
{
var nodeId = Int32.Parse(node.NodeID);
if (OrgnizationService.Count(p => p.org_parent_id == nodeId && p.deleteflag == 0) == 0)
{
node.Leaf = true;
}
else
{
node.Expanded = true;
node.Nodes.Clear();
foreach (
base_orgnization orgnization in
OrgnizationService.Where(p => p.org_parent_id == nodeId && p.deleteflag == 0))
{
var cNode = new TreeNode
{
Text = orgnization.org_name,
NodeID = orgnization.id.ToString(CultureInfo.InvariantCulture),
EnablePostBack = true
};
//加载子部门信息
node.Nodes.Add(cNode);
}
}
}
/// <summary>
/// 获取选中的ID集合
/// </summary>
/// <returns></returns>
private IEnumerable<int> GetSelectIds()
{
int[] selections = Grid1.SelectedRowIndexArray;
var selectIds = new int[selections.Length];
for (int i = 0; i < selections.Length; i++)
{
selectIds[i] = Int32.Parse(Grid1.DataKeys[selections[i]][0].ToString());
}
return selectIds;
}
/// <summary>
/// 绑定数据表格
/// </summary>
private void BindDataGrid()
{
int output;
dynamic orderingSelector;
Expression<Func<base_account, bool>> predicate = BuildPredicate(out orderingSelector);
//取数据源
IQueryable<base_account> list = AccountService.Where(predicate, Grid1.PageSize, Grid1.PageIndex + 1,
orderingSelector, EnumHelper.ParseEnumByString<OrderingOrders>(SortDirection), out output);
//设置页面大小
Grid1.RecordCount = output;
//绑定数据源
Grid1.DataSource = list;
Grid1.DataBind();
ddlPageSize.SelectedValue = Grid1.PageSize.ToString(CultureInfo.InvariantCulture);
}
作者:
leetle
时间:
2013-10-22 11:06
本帖最后由 leetle 于 2013-10-22 11:14 编辑
/// <summary>
/// 创建查询条件表达式和排序表达式
/// </summary>
/// <param name="orderingSelector"></param>
/// <returns></returns>
private Expression<Func<base_account, bool>> BuildPredicate(out dynamic orderingSelector)
{
// 查询条件表达式
Expression expr = Expression.Constant(true);
return predicate;
}
#endregion
}
}
欢迎光临 FineUI 官方论坛 (https://fineui.com/bbs/)
Powered by Discuz! X3.4