似乎我错过了一些简单的事情 更改PageIndex时,选中的框不保留状态。
.aspx page
<asp:Panel ID="ReassignPanel" runat="server" Width="100%" >
</asp:Panel>
背后的代码
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TasksReassign2_aspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvTasksList.Visible = true;
DropDownList ddlAssignedTo = (DropDownList)ReassignDetailsView.FindControl("ddlUserAssignedToSelection");
ddlAssignedTo.DataSource = DropDownSource_AssignedTo;
ddlAssignedTo.DataBind();
for (int i = 0; i < ddlAssignedTo.Items.Count; i++)
{
ddlAssignedTo.Items[i].Text = ddlAssignedTo.Items[i].Text.Replace(" (inactive)", "");
}
DropDownList ddlReassign = (DropDownList)ReassignDetailsView.FindControl("ddlUserReassignSelection");
ddlReassign.DataSource = DropDownSource_ReassignTo;
ddlReassign.DataBind();
ddlReassign.SelectedIndex = ddlReassign.Items.IndexOf(ddlReassign.Items.FindByValue(usr.UserID.ToString()));
}
}
private void CheckboxSave()
{
ArrayList array_Tasks = new ArrayList();
for (int index = 0; index < gvTasksList.Rows.Count; index++)
{
bool result = ((CheckBox)gvTasksList.Rows[index].FindControl("cbReassign")).Checked;
if (ViewState["SelectedRecords"] != null)
{
array_Tasks = (ArrayList)ViewState["SelectedRecords"];
}
if (result)
{
if (!array_Tasks.Contains(gvTasksList.Rows[index].Cells[1].Text))
{
array_Tasks.Add(gvTasksList.Rows[index].Cells[1].Text);
}
}
else
{
array_Tasks.Remove(gvTasksList.Rows[index].Cells[1].Text);
}
}
if (array_Tasks != null && array_Tasks.Count > 0) ViewState["SelectedRecords"] = array_Tasks;
}
private void CheckboxPopulate()
{
ArrayList array_Tasks = (ArrayList)ViewState["SelectedRecords"];
if (array_Tasks != null && array_Tasks.Count > 0)
{
for (int index = 0; index < gvTasksList.Rows.Count; index++)
{
if (array_Tasks.Contains(gvTasksList.Rows[index].Cells[1].Text))
{
CheckBox chkbx = (CheckBox)gvTasksList.Rows[index].FindControl("cbReassign");
chkbx.Checked = true;
}
}
}
}
protected void paging_gvTasks(object sender, GridViewPageEventArgs e)
{
CheckboxSave();
gvTasksList.PageIndex = e.NewPageIndex;
gvTasksList.DataBind();
CheckboxPopulate();
}
/****************************************************************************/
protected void evaluateCheckChange(object sender, EventArgs e)
{
if (rblOption.Text == "T") //Taks
{
gvTasksList.PageSize = Convert.ToInt32(((RadioButton)sender).Text);
}
}
protected void Changed_AssignedTo_or_rblOption(object sender, EventArgs e)
{
DropDownList ddlAssignedTo = (DropDownList)ReassignDetailsView.FindControl("ddlUserAssignedToSelection");
ddlAssignedTo.DataSource = DropDownSource_AssignedTo;
UserTasksGridSource.SelectParameters.Clear();
gvTasksList.Visible = false;
if (rblOption.Text == "T")
{
gvTasksList.Visible = true;
LabelHeaderTasks.Text = "Task(s)";
UserTasksGridSource.SelectParameters.Clear();
UserTasksGridSource.SelectParameters.Add("Users_PKID_assigned", TypeCode.Int32, ddlAssignedTo.SelectedValue);
UserTasksGridSource.SelectMethod = "Tasks_GetListByUsers_PKID_assigned_to";
UserTasksGridSource.DataBind();
}
}
}
答案 0 :(得分:0)
切换GridView的Page Index会导致回发。除非您在回发之间存储和加载复选框值,否则您需要使用UpdatePanel(AJAX)和相应的AsyncPostbackTrigger。
http://msdn.microsoft.com/en-us/library/vstudio/bb386454(v=vs.100).aspx http://www.asp.net/AJAX/documentation/live/overview/UpdatePanelOverview.aspx http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers
更新:
抱歉,我应该已阅读您的示例代码。我没有意识到你存储了复选框值。
但是,查看paging_gvTasks
中的代码,我认为它不会起作用。页面将在进入分页事件之前重新加载。断开页面加载和分页事件。然后更改GridView的页面并观察会发生什么。
页面生命周期:http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx