Gridview 分页复选框未正确分配
Gridview Paging checkbox not assigning properly
我的 gridview
和 pagesize = 2
中有数据。此外,gridview 中的每一行都有一个 approve/reject.
的复选框
如果我检查了第 1 页的 2 行和第 2 页的 2 行,它只会批准从第 2 页检查的最后两页。为什么会这样我不知道。我希望所有 4 条记录都获得批准。
下面是我写的代码:
ASPX
<asp:GridView ID="grdDisplayCMMData" runat="server"
AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true"
CssClass="heavyTable table" EmptyDataText="No records to display"
AllowPaging="true" PageSize="2"
OnPageIndexChanging="grdDisplayCMMData_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID_CMM" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SAP ID">
<ItemTemplate>
<asp:Label ID="lblSAP_ID_CMM" runat="server" Text='<%#Eval("SAP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE">
<ItemTemplate>
<asp:Label ID="lblID_OD_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("ID_OD_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblID_OD_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("ID_OD_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblRRH_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("RRH_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblRRH_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("RRH_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("TENANCY_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("TENANCY_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
<asp:Label ID="lblSTATUS_CMM" runat="server" Text='<%#Eval("STATUS") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CREATED BY" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCREATEDBY_CMM" runat="server" Text='<%#Eval("CREATED_BY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:CheckBox ID="chkApprRejCMM" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#代码:
protected void btnApproveCMM_Click(object sender, EventArgs e)
{
try
{
bool flgCMM = false;
IPColoFields ObjIPColoFields = new App_Code.IPColoFields();
List<IPColoBilling_BKP.App_Code.UMS.UMSGroupDetails> UMSGroupDetails = (List<IPColoBilling_BKP.App_Code.UMS.UMSGroupDetails>)Session["lstUMSGroupDetails"];
Session["lstUMSGroupDetails"] = UMSGroupDetails;
string strApprove = "";
foreach (GridViewRow gvrow in grdDisplayCMMData.Rows)
{
var checkbox = gvrow.FindControl("chkApprRejCMM") as CheckBox;
if (checkbox.Checked)
{
flgCMM = true;
int Id = Convert.ToInt32((grdDisplayCMMData.Rows[gvrow.RowIndex].FindControl("lblID_CMM") as Label).Text);
ObjIPColoFields.Unique_Id = Id;
ObjIPColoFields.UMS_GRP_BY_ID = intCurrentGrpId;
ObjIPColoFields.UMS_GRP_BY_NAME = strCurrentGrp;
ObjIPColoFields.UMS_GRP_TO_ID = UMSGroupDetails[1].GroupID;
ObjIPColoFields.UMS_GRP_TO_NAME = UMSGroupDetails[1].GroupName;
ObjIPColoFields.FCA_STATUS = "1";
ObjIPColoFields.LAST_UPDATED_BY = lblUserName.Text;
strApprove = CommonDB.Approve_IPCOLO_CMMLevel(ObjIPColoFields);
}
}
BindCMMData();
if (flgCMM == false)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please check atleast one row'); window.location ='IpColoDefault.aspx';", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Record Approved successfully'); window.location ='IpColoDefault.aspx';", true);
}
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["IPCOLO_LOG"].ToString());
}
}
另见分页代码
protected void grdDisplayCMMData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
BindCMMData();
grdDisplayCMMData.PageIndex = e.NewPageIndex;
grdDisplayCMMData.DataBind();
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["IPCOLO_LOG"].ToString());
}
}
好吧,我可以想出 2 种解决问题的方法。
更改您的页面事件以显示/隐藏项目。
默认网格视图分页事件会触发回发并重新绑定网格,因此您无法访问上一页上的复选框,它们只是 "dissapear"。现在,您可以解决这个问题,方法是加载完整的网格,然后将不在您定义的页面范围内的项目的可见性设置为不可见。但这对海量数据来说非常费力,我不推荐这种解决方案。
第二种解决方案是保存复选框状态,假设是 ViewState
这样,您应该为您的复选框实现一个 OnCheckedChanged
事件,该事件会在选中索引的视图状态中添加/删除自身。现在您还必须实现一个 OnItemDataBound
事件以从您的视图状态呈现复选框状态。最后一步是在您的按钮中单击以遍历视图状态并对每个项目执行您的逻辑。
<asp:GridView ID="grdDisplayCMMData" runat="server"
AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true"
CssClass="heavyTable table" EmptyDataText="No records to display"
AllowPaging="true" PageSize="2"
OnPageIndexChanging="grdDisplayCMMData_PageIndexChanging">
OnItemDataBound="grdDisplayCMMData_OnItemDataBound"
<Columns>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:CheckBox ID="chkApprRejCMM" runat="server" OnCheckedChange="chkApprRejCMM_OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void chkApprRejCMM_OnCheckedChanged(object sender, EventArgs e)
{
if(ViewState["CheckedCheckboxes"] != null)
{
var CheckedCheckboxes = (List<int>)ViewState["CheckedCheckboxes"];
var checkbox = (CheckBox)sender;
var idLabel = (TextBox)checkbox.BindingContainer.FindControl("lblID_CMM");
if(idLabel != null && !string.IsNullOrEmpty(idLabel.Text))
{
if(checkbox.Checked)
{
if(!CheckedCheckboxes.Contains(Convert.ToInt32(idLabel.Text)))
CheckedCheckboxes.Add(Convert.ToInt32(idLabel.Text));
}
else
{
if(CheckedCheckboxes.Contains(Convert.ToInt32(idLabel.Text)))
CheckedCheckboxes.Remove(Convert.ToInt32(idLabel.Text))
}
}
ViewState["CheckedCheckboxes"] = CheckedCheckboxes;
}
}
protected void grdDisplayCMMData_OnItemDataBound(object sender, GridViewEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(ViewState["CheckedCheckboxes"] != null)
{
var CheckedCheckboxes = (List<int>)ViewState["CheckedCheckboxes"];
var checkbox = (CheckBox)e.Row.FindControl("lchkApprRejCMM");
var idLabel = (TextBox)e.Row.FindControl("lblID_CMM");
if(idLabel != null && checkbox != null && !string.IsNullOrEmpty(idLabel.Text))
{
if(CheckedCheckboxes.Contains(Convert.ToInt32(idLabel.Text)))
checbox.Checked = true;
else
checbox.Checked = false;
}
}
}
}
protected void YourButtonClick(object sender, EventArgs e)
{
if(ViewState["CheckedCheckboxes"] != null)
{
var CheckedCheckboxes = (List<int>)ViewState["CheckedCheckboxes"];
foreach (var id in CheckedCheckboxes)
{
//perform logic with id
}
}
}
编辑 1:我在不使用 IDE
的情况下编写代码时遗漏了一些错误
我的 gridview
和 pagesize = 2
中有数据。此外,gridview 中的每一行都有一个 approve/reject.
如果我检查了第 1 页的 2 行和第 2 页的 2 行,它只会批准从第 2 页检查的最后两页。为什么会这样我不知道。我希望所有 4 条记录都获得批准。
下面是我写的代码:
ASPX
<asp:GridView ID="grdDisplayCMMData" runat="server"
AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true"
CssClass="heavyTable table" EmptyDataText="No records to display"
AllowPaging="true" PageSize="2"
OnPageIndexChanging="grdDisplayCMMData_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID_CMM" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SAP ID">
<ItemTemplate>
<asp:Label ID="lblSAP_ID_CMM" runat="server" Text='<%#Eval("SAP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE">
<ItemTemplate>
<asp:Label ID="lblID_OD_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("ID_OD_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblID_OD_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("ID_OD_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblRRH_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("RRH_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblRRH_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("RRH_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("TENANCY_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("TENANCY_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
<asp:Label ID="lblSTATUS_CMM" runat="server" Text='<%#Eval("STATUS") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CREATED BY" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCREATEDBY_CMM" runat="server" Text='<%#Eval("CREATED_BY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:CheckBox ID="chkApprRejCMM" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#代码:
protected void btnApproveCMM_Click(object sender, EventArgs e)
{
try
{
bool flgCMM = false;
IPColoFields ObjIPColoFields = new App_Code.IPColoFields();
List<IPColoBilling_BKP.App_Code.UMS.UMSGroupDetails> UMSGroupDetails = (List<IPColoBilling_BKP.App_Code.UMS.UMSGroupDetails>)Session["lstUMSGroupDetails"];
Session["lstUMSGroupDetails"] = UMSGroupDetails;
string strApprove = "";
foreach (GridViewRow gvrow in grdDisplayCMMData.Rows)
{
var checkbox = gvrow.FindControl("chkApprRejCMM") as CheckBox;
if (checkbox.Checked)
{
flgCMM = true;
int Id = Convert.ToInt32((grdDisplayCMMData.Rows[gvrow.RowIndex].FindControl("lblID_CMM") as Label).Text);
ObjIPColoFields.Unique_Id = Id;
ObjIPColoFields.UMS_GRP_BY_ID = intCurrentGrpId;
ObjIPColoFields.UMS_GRP_BY_NAME = strCurrentGrp;
ObjIPColoFields.UMS_GRP_TO_ID = UMSGroupDetails[1].GroupID;
ObjIPColoFields.UMS_GRP_TO_NAME = UMSGroupDetails[1].GroupName;
ObjIPColoFields.FCA_STATUS = "1";
ObjIPColoFields.LAST_UPDATED_BY = lblUserName.Text;
strApprove = CommonDB.Approve_IPCOLO_CMMLevel(ObjIPColoFields);
}
}
BindCMMData();
if (flgCMM == false)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please check atleast one row'); window.location ='IpColoDefault.aspx';", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Record Approved successfully'); window.location ='IpColoDefault.aspx';", true);
}
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["IPCOLO_LOG"].ToString());
}
}
另见分页代码
protected void grdDisplayCMMData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
BindCMMData();
grdDisplayCMMData.PageIndex = e.NewPageIndex;
grdDisplayCMMData.DataBind();
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["IPCOLO_LOG"].ToString());
}
}
好吧,我可以想出 2 种解决问题的方法。
更改您的页面事件以显示/隐藏项目。
默认网格视图分页事件会触发回发并重新绑定网格,因此您无法访问上一页上的复选框,它们只是 "dissapear"。现在,您可以解决这个问题,方法是加载完整的网格,然后将不在您定义的页面范围内的项目的可见性设置为不可见。但这对海量数据来说非常费力,我不推荐这种解决方案。
第二种解决方案是保存复选框状态,假设是
ViewState
这样,您应该为您的复选框实现一个
OnCheckedChanged
事件,该事件会在选中索引的视图状态中添加/删除自身。现在您还必须实现一个OnItemDataBound
事件以从您的视图状态呈现复选框状态。最后一步是在您的按钮中单击以遍历视图状态并对每个项目执行您的逻辑。<asp:GridView ID="grdDisplayCMMData" runat="server" AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true" CssClass="heavyTable table" EmptyDataText="No records to display" AllowPaging="true" PageSize="2" OnPageIndexChanging="grdDisplayCMMData_PageIndexChanging"> OnItemDataBound="grdDisplayCMMData_OnItemDataBound" <Columns> <asp:TemplateField HeaderText="Approve/Reject"> <ItemTemplate> <asp:CheckBox ID="chkApprRejCMM" runat="server" OnCheckedChange="chkApprRejCMM_OnCheckedChanged" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void chkApprRejCMM_OnCheckedChanged(object sender, EventArgs e) { if(ViewState["CheckedCheckboxes"] != null) { var CheckedCheckboxes = (List<int>)ViewState["CheckedCheckboxes"]; var checkbox = (CheckBox)sender; var idLabel = (TextBox)checkbox.BindingContainer.FindControl("lblID_CMM"); if(idLabel != null && !string.IsNullOrEmpty(idLabel.Text)) { if(checkbox.Checked) { if(!CheckedCheckboxes.Contains(Convert.ToInt32(idLabel.Text))) CheckedCheckboxes.Add(Convert.ToInt32(idLabel.Text)); } else { if(CheckedCheckboxes.Contains(Convert.ToInt32(idLabel.Text))) CheckedCheckboxes.Remove(Convert.ToInt32(idLabel.Text)) } } ViewState["CheckedCheckboxes"] = CheckedCheckboxes; } } protected void grdDisplayCMMData_OnItemDataBound(object sender, GridViewEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if(ViewState["CheckedCheckboxes"] != null) { var CheckedCheckboxes = (List<int>)ViewState["CheckedCheckboxes"]; var checkbox = (CheckBox)e.Row.FindControl("lchkApprRejCMM"); var idLabel = (TextBox)e.Row.FindControl("lblID_CMM"); if(idLabel != null && checkbox != null && !string.IsNullOrEmpty(idLabel.Text)) { if(CheckedCheckboxes.Contains(Convert.ToInt32(idLabel.Text))) checbox.Checked = true; else checbox.Checked = false; } } } } protected void YourButtonClick(object sender, EventArgs e) { if(ViewState["CheckedCheckboxes"] != null) { var CheckedCheckboxes = (List<int>)ViewState["CheckedCheckboxes"]; foreach (var id in CheckedCheckboxes) { //perform logic with id } } }
编辑 1:我在不使用 IDE
的情况下编写代码时遗漏了一些错误