Я попробовал методы PreRender от @Chuck, но у меня это не сработало (VS2005, ASP.net2, SQLsrv2005).Возможно, слишком старая технология, но это то, что есть у клиента.
Поэтому я попробовал простую технику myaspsnippets , и с некоторыми модификациями она отлично работает!
My Gridview
<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>
Используемые мной методы Pageing:
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 с cbSEND на то, что вы используете, и указатьHiddenFields для некоторого другого элемента управления или значения, которое имеет уникальный идентификатор для ваших строк. Не используйте RowIndex
, так как это не уникально по всей длине данных в GridView.
Работает как шарм!