我尝试了@Chuck 的 PreRender 技术,但它对我不起作用(VS2005、ASP.net2、SQLsrv2005)。也许技术太老了,但这是客户所拥有的。
所以我尝试了简单的myaspsnippets技术,并通过一些修改它完美地工作!
我的网格视图
<asp:GridView ID="gvFTUNSENT" runat="server"
AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="False" CssClass="gvCSS" Width="100%"
DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT"
GridLines="None" AllowPaging="True" PageSize="10" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
OnPageIndexChanged="GridView_PageIndexChanged"
OnPageIndexChanging="GridView_PageIndexChanging">
<RowStyle Wrap="True" Height="48px" />
<Columns>
...etc...
</Columns>
<FooterStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" Height="100%" />
<PagerStyle CssClass="cssPager" BackColor="#6B696B" ForeColor="White" HorizontalAlign="Left" Height="100%" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
</asp:GridView>
我使用的分页方法是:
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
//saves a "copy" of the page before it's changed over to the next one
SavePageState(gv);
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
和
protected void GridView_PageIndexChanged(object sender, EventArgs e)
{
...your code to handle anything after the page has changed
gv.DataSource = dt;
gv.DataSourceID = null;
gv.DataBind();
//reload any checkboxes that were session saved in the page
LoadPageState(gv);
}
}
}
}
所以 SAVE 和 LOAD 方法如下:
private void SavePageState(GridView gv)
{
ArrayList categoryIDList = new ArrayList();
Int32 index = -1;
foreach (GridViewRow row in gv.Rows)
{
HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID");
if (hfStudentUnitID != null)
{
if (hfStudentUnitID.Value.Length > 0)
{
index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"];
bool result = ((CheckBox)row.FindControl("cbSEND")).Checked;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
}
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
和
private void LoadPageState(GridView gv)
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in gv.Rows)
{
HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID");
if (hfStudentUnitID != null)
{
if (hfStudentUnitID.Value.Length > 0)
{
Int32 index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"];
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("cbSEND");
myCheckBox.Checked = true;
}
}
}
}
}
}
为了使这项工作适合您,您需要将 Paging 方法调用放入您的 GridView,将 CheckBox ID 从 cbSEND 更改为您使用的任何内容,并将 HiddenFields 指向其他具有唯一标识符的控件或值为您的行。不要使用RowIndex
,因为它在 GridView 中的整个数据长度中并不是唯一的。
奇迹般有效!