FineUI/FineUI.Examples/grid/grid_paging_selection.aspx.cs

126 lines
3.7 KiB
C#
Raw Normal View History

2015-05-19 14:45:47 +08:00
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace FineUI.Examples.grid
{
public partial class grid_paging_selection : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
#region LoadData
private void BindGrid()
{
2016-01-10 01:19:30 -05:00
DataTable table = DataSourceUtil.GetDataTable2();
2015-05-19 14:45:47 +08:00
Grid1.DataSource = table;
Grid1.DataBind();
}
#endregion
#region Events
protected void Button1_Click(object sender, EventArgs e)
{
2017-09-05 10:49:48 +08:00
SyncSelectedRowIDArrayToHiddenField();
2015-05-19 14:45:47 +08:00
labResult.Text = "选中行的ID列表为" + hfSelectedIDS.Text.Trim();
}
2017-09-05 10:49:48 +08:00
2016-01-10 01:19:30 -05:00
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
SyncSelectedRowIDArrayToHiddenField(e.OldPageIndex);
2015-05-19 14:45:47 +08:00
2017-09-05 10:49:48 +08:00
UpdateSelectedRowIDArray(e.NewPageIndex);
2015-05-19 14:45:47 +08:00
}
2017-09-05 10:49:48 +08:00
private List<string> GetSelectedRowIDArrayFromHiddenField()
2015-05-19 14:45:47 +08:00
{
JArray idsArray = new JArray();
string currentIDS = hfSelectedIDS.Text.Trim();
if (!String.IsNullOrEmpty(currentIDS))
{
idsArray = JArray.Parse(currentIDS);
}
else
{
idsArray = new JArray();
}
return new List<string>(idsArray.ToObject<string[]>());
}
2017-09-05 10:49:48 +08:00
private void SyncSelectedRowIDArrayToHiddenField()
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
SyncSelectedRowIDArrayToHiddenField(Grid1.PageIndex);
}
2015-05-19 14:45:47 +08:00
2017-09-05 10:49:48 +08:00
private void SyncSelectedRowIDArrayToHiddenField(int pageIndex)
{
List<string> ids = GetSelectedRowIDArrayFromHiddenField();
List<string> selectedRowIDs = new List<string>(Grid1.SelectedRowIDArray);
2015-05-19 14:45:47 +08:00
2017-09-05 10:49:48 +08:00
// 当前页的开始序号和结束序号
int startPageIndex = pageIndex * Grid1.PageSize;
int endPageIndex = Math.Min(startPageIndex + Grid1.PageSize, Grid1.RecordCount) - 1;
for (int i = startPageIndex; i <= endPageIndex; i++)
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
string rowID = Grid1.Rows[i].RowID;
if (selectedRowIDs.Contains(rowID))
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
if (!ids.Contains(rowID))
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
ids.Add(rowID);
2015-05-19 14:45:47 +08:00
}
}
else
{
2017-09-05 10:49:48 +08:00
if (ids.Contains(rowID))
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
ids.Remove(rowID);
2015-05-19 14:45:47 +08:00
}
}
}
2017-09-05 10:49:48 +08:00
2015-05-19 14:45:47 +08:00
hfSelectedIDS.Text = new JArray(ids).ToString(Formatting.None);
}
2017-09-05 10:49:48 +08:00
private void UpdateSelectedRowIDArray(int pageIndex)
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
List<string> ids = GetSelectedRowIDArrayFromHiddenField();
2015-05-19 14:45:47 +08:00
2017-09-05 10:49:48 +08:00
List<string> selectedRowIDs = new List<string>();
// 当前页的开始序号和结束序号
int startPageIndex = pageIndex * Grid1.PageSize;
int endPageIndex = Math.Min(startPageIndex + Grid1.PageSize, Grid1.RecordCount) - 1;
for (int i = startPageIndex; i <= endPageIndex; i++)
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
string rowID = Grid1.Rows[i].RowID;
if (ids.Contains(rowID))
2015-05-19 14:45:47 +08:00
{
2017-09-05 10:49:48 +08:00
selectedRowIDs.Add(rowID);
2015-05-19 14:45:47 +08:00
}
}
2017-09-05 10:49:48 +08:00
Grid1.SelectedRowIDArray = selectedRowIDs.ToArray();
}
2015-05-19 14:45:47 +08:00
#endregion
}
}