我的gridview
和pagesize = 2
中都有数据。此外,gridview中的每一行都有一个复选框,用于批准/拒绝。
如果我从第一页检查了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());
}
}
答案 0 :(得分:1)
好吧,我可以想到两种方法来解决问题。
更改页面事件以显示/隐藏项目。
默认的网格视图分页事件触发回发并重新绑定网格,因此,您无法访问上一页的复选框,它们只是“消失”。现在,您可以解决此问题,方法是加载整个网格,然后将不在您定义的页面范围内的项目的可见性设置为不可见。但这对于海量数据变得非常繁重,因此我不推荐这种解决方案。
第二种解决方案是保存复选框状态,用ViewState
这样,您应该为自己的checbox实现一个OnCheckedChanged
事件,该事件将在已检查索引的视图状态中添加/删除自身。现在,您还必须实现一个OnItemDataBound
事件,以从viewstate呈现复选框状态。最后一步是单击按钮以遍历视图状态并在每个项目上执行逻辑。
<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的情况下编写代码遗漏了一些错误