188 lines
5.5 KiB
C#
188 lines
5.5 KiB
C#
using BLL;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Web;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
|
||
namespace FineUIPro.Web.Email_Send
|
||
{
|
||
public partial class SelectUserControl : System.Web.UI.UserControl
|
||
{
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack)
|
||
{
|
||
BindGrid("");
|
||
DataBindDrpUnit();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
#region BindGrid
|
||
|
||
|
||
private void BindGrid(string selectUnitId)
|
||
{
|
||
// 1.设置总项数(特别注意:数据库分页一定要设置总记录数RecordCount)
|
||
Grid1.RecordCount = GetTotalCount(selectUnitId);
|
||
|
||
// 2.获取当前分页数据
|
||
DataTable table = GetPagedDataTable(selectUnitId);
|
||
|
||
// 3.绑定到Grid
|
||
Grid1.DataSource = table;
|
||
Grid1.DataBind();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 模拟返回总项数
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private int GetTotalCount(string selectUnitId)
|
||
{
|
||
return GetSource(selectUnitId).Rows.Count;
|
||
}
|
||
|
||
private void DataBindDrpUnit()
|
||
{
|
||
var data = Funs.DB.Base_Unit.ToList();
|
||
drpSearch.DataSource = data;
|
||
drpSearch.DataTextField = "UnitName";
|
||
drpSearch.DataValueField = "UnitId";
|
||
drpSearch.DataBind();
|
||
drpSearch.Items.Insert(0,new ListItem("请选择单位",""));
|
||
}
|
||
/// <summary>
|
||
/// 模拟数据库分页(实际项目中请直接使用SQL语句返回分页数据!)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private DataTable GetPagedDataTable(string selectUnitId)
|
||
{
|
||
int pageIndex = Grid1.PageIndex;
|
||
int pageSize = Grid1.PageSize;
|
||
|
||
DataTable table = GetSource(selectUnitId);
|
||
|
||
DataTable paged = table.Clone();
|
||
int rowbegin = pageIndex * pageSize;
|
||
int rowend = (pageIndex + 1) * pageSize;
|
||
if (rowend > table.Rows.Count)
|
||
{
|
||
rowend = table.Rows.Count;
|
||
}
|
||
for (int i = rowbegin; i < rowend; i++)
|
||
{
|
||
paged.ImportRow(table.Rows[i]);
|
||
}
|
||
|
||
return paged;
|
||
}
|
||
protected void drpSearch_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
string rowId = this.drpSearch.SelectedValue;
|
||
this.BindGrid(rowId);
|
||
}
|
||
private DataTable GetSource(string selectUnitId)
|
||
{
|
||
string sortField = Grid1.SortField;
|
||
string sortDirection = Grid1.SortDirection;
|
||
|
||
string strSql = @"select a.UserId,b.UnitId,b.UserName,c.UnitName,b.Email from Project_User as a inner join Sys_User as b on a.UserId=b.UserId
|
||
inner join Base_Unit as c on b.UnitId=c.UnitId where b.IsPost=1 order by c.UnitName ";
|
||
List<SqlParameter> listStr = new List<SqlParameter>();
|
||
SqlParameter[] parameter = listStr.ToArray();
|
||
DataTable table2 = SQLHelper.GetDataTableRunText(strSql, parameter);
|
||
|
||
DataView view2 = table2.DefaultView;
|
||
|
||
List<string> filters = new List<string>();
|
||
|
||
string searchKeyword = ttbSearch.Text.Trim();
|
||
if (!String.IsNullOrEmpty(searchKeyword) && ttbSearch.ShowTrigger1)
|
||
{
|
||
// RowFilter的用法:http://www.csharp-examples.net/dataview-rowfilter/
|
||
filters.Add(String.Format("(UnitName LIKE '*{0}*' or UserName like '*{0}*')", EscapeLikeValue(searchKeyword)));
|
||
}
|
||
if (!string.IsNullOrEmpty(selectUnitId))
|
||
{
|
||
filters.Add(string.Format(" UnitId='{0}'", selectUnitId));
|
||
}
|
||
|
||
if (filters.Count > 0)
|
||
{
|
||
view2.RowFilter = String.Join(" AND ", filters.ToArray());
|
||
}
|
||
|
||
|
||
|
||
return view2.ToTable();
|
||
}
|
||
|
||
// From: http://www.csharp-examples.net/dataview-rowfilter/
|
||
public static string EscapeLikeValue(string valueWithoutWildcards)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < valueWithoutWildcards.Length; i++)
|
||
{
|
||
char c = valueWithoutWildcards[i];
|
||
if (c == '*' || c == '%' || c == '[' || c == ']')
|
||
sb.Append("[").Append(c).Append("]");
|
||
else if (c == '\'')
|
||
sb.Append("''");
|
||
else
|
||
sb.Append(c);
|
||
}
|
||
return sb.ToString();
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region Events
|
||
|
||
|
||
|
||
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
|
||
{
|
||
//Grid1.PageIndex = e.NewPageIndex;
|
||
|
||
BindGrid(string.Empty); ;
|
||
}
|
||
|
||
protected void Grid1_Sort(object sender, GridSortEventArgs e)
|
||
{
|
||
//Grid1.SortDirection = e.SortDirection;
|
||
//Grid1.SortField = e.SortField;
|
||
|
||
BindGrid(string.Empty);
|
||
}
|
||
|
||
|
||
protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
|
||
{
|
||
ttbSearch.Text = String.Empty;
|
||
ttbSearch.ShowTrigger1 = false;
|
||
|
||
BindGrid(string.Empty);
|
||
}
|
||
|
||
protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
|
||
{
|
||
ttbSearch.ShowTrigger1 = true;
|
||
|
||
BindGrid(string.Empty);
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
}
|
||
} |