在GridView中的分页期间维护复选框的状态
大家好,
我成功地将代码添加到页面中,以在分页期间维护网格视图中复选框的状态,但是出现了一个非常不同的问题.如果我在第一页上选中了一些复选框,然后在第二页上选中了某些复选框,然后返回到第一页,那么我看到的是,与我相比,自动选中的复选框数量更多.我不知道我要去哪里错了!
我完全按照此链接中的逻辑和代码片段进行操作:
维护选定复选框的状态在GridView内部的不同页面中 [ ^ ]
谁能帮助我,这对我的项目来说确实非常紧急和重要.
谢谢
Hi All,
I successfully added the code to my page for maintaining the state of check boxes in grid view during pagination but got a very different issue. if I select some check boxes on the first page and than go on second page and select some check boxes there and return back to first page, what I see is that there are more number of check boxes selected automatically as compared to I did. I don''t know where I am going wrong !
I exactly followed the logic and code snippet as in this link :
Maintaining States of Selected CheckBoxes in Different Pages inside the GridView[^]
Can anyone please help me,,,It is really urgent and important for my project.
Thank you
可以用多种方法来完成,但是基本上每次核对CheckBox时,都必须将其状态保存到现在为Checked或Unchecked的位置.然后,在每次重新绑定数据后,您可以将该状态从Session或ViewState中拉出,并重新设置CheckBoxes的状态.这是我如何维护状态的示例:
ASPX:
This can be done a variety of ways, but basically everytime a CheckBox is checked, you must save its state somewhere that it is now Checked or Unchecked. Then after every re-bind of your data, you can pull this state out of the Session or ViewState and re-set the state of the CheckBoxes. Here''s an example of how I would maintain the state:
ASPX:
<asp:gridview id="gvProducts" runat="server" allowpaging="True" autogeneratecolumns="False" xmlns:asp="#unknown">
datakeynames="ProductID" datasourceid="ldsProducts" style="margin-top: 20px;"
onpageindexchanging="gvProducts_PageIndexChanging" onrowdatabound="gvProducts_RowDataBound">
<columns>
<asp:templatefield headertext="Select">
<itemtemplate>
<asp:checkbox id="chkSelect" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False">
readonly="True" sortexpression="ProductID" />
<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
</asp:boundfield></columns>
</asp:gridview>
<asp:linqdatasource id="ldsProducts" runat="server" contexttypename="LinqToSql.NorthwindDataContext" xmlns:asp="#unknown">
tablename="Products">
</asp:linqdatasource>
背后的代码:
Code Behind:
public partial class GridView_MaintainCheckBoxStateWhenPaging : System.Web.UI.Page
{
private List<int> ProductIDs
{
get
{
if (this.ViewState["ProductIDs"] == null)
{
this.ViewState["ProductIDs"] = new List<int>();
}
return this.ViewState["ProductIDs"] as List<int>;
}
}
protected void SelectDeselect(object sender, CommandEventArgs e)
{
foreach (GridViewRow gvr in gvProducts.Rows)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect != null)
{
chkSelect.Checked = e.CommandName.Equals("SelectAll");
}
}
}
protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
foreach (GridViewRow gvr in gvProducts.Rows)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect != null)
{
int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
if (chkSelect.Checked && !this.ProductIDs.Contains(productID))
{
this.ProductIDs.Add(productID);
}
else if (!chkSelect.Checked && this.ProductIDs.Contains(productID))
{
this.ProductIDs.Remove(productID);
}
}
}
}
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect != null)
{
int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
chkSelect.Checked = this.ProductIDs.Contains(productID);
}
}
}
}
祝一切顺利.
--Amit
All the best.
--Amit
protected void btn_daily_send_all_Click(object sender, EventArgs e)
{
int flag = 0;
gvAllClients.Visible = true;
gvAllClients.AllowPaging = false; // this is where is disabled paging
try
{
if ((txt_daily_from.Text != "") || (txt_daily_to.Text != ""))
{
string strSucessEmail = "";
string strFailedEmail = "";
/* this is my some logic of checking date validations*/
DateTime date_from_for_daily_email = Convert.ToDateTime(txt_daily_from.Text);
DateTime date_to_for_daily_email = Convert.ToDateTime(txt_daily_to.Text);
DateTime date_end = Convert.ToDateTime(ViewState["latest_date_port_uploaded"]);
DateTime dt = Convert.ToDateTime(ViewState["date_last_mailed"]);
DateTime date_start = choosing_first_date(ref dt);
//here i traversed my gv
foreach(GridViewRow row in gvAllClients.Rows)
{
string strworkOrder = ((Label)(row.Cells[5].FindControl("lblWorkOrd"))).Text;
string strdata = ((Label)(row.Cells[6].FindControl("lblData"))).Text;
string strHSCODE = ((Label)(row.Cells[9].FindControl("lblHSCode"))).Text;
string strCombination = ((Label)(row.Cells[10].FindControl("lblComb"))).Text;
string strProd = ((Label)(row.Cells[11].FindControl("lblProd"))).Text;
string strProd_Not = ((Label)(row.Cells[12].FindControl("lblProd_not"))).Text;
string strActive = ((Label)(row.Cells[13].FindControl("lblActive"))).Text;
string strEmailId = ((Label)(row.Cells[14].FindControl("lblEmail"))).Text;
DateTime date_from_subscp = Convert.ToDateTime(((Label)(row.Cells[7].FindControl("lblfrom"))).Text);
DateTime date_to_subscp = Convert.ToDateTime(((Label)(row.Cells[8].FindControl("lblTo"))).Text);
CheckBox chk = (CheckBox)(row.Cells[30].FindControl("chkEmail_Single"));
if (strActive == "NO")
{
strFailedEmail += "" + strEmailId + ", ";
}
else if (strActive == "YES")
{
//((date_from_for_daily_email < date_start) ||
if (date_to_for_daily_email > date_end)
{
lblMsg.Visible = true;
lblMsg.Text = "You cannnot select a date less than last mailed date and greater than " + date_end.ToString("dd-MMM-yyyy") + ".";
}
else if (date_from_for_daily_email > date_to_for_daily_email)
{
lblMsg.Visible = true;
lblMsg.Text = "Invalid date range. Please check the dates and than try again...";
}
else if (date_to_subscp < date_to_for_daily_email)
{
// this is some of my logic to store the records of success and unsuccessful mails
strFailedEmail += "" + strEmailId + ", ";
}
else if (chk.Checked == true)
{
strFailedEmail += "" + strEmailId + ", ";
}
else
{
int iResult = bl_Query_for_admin.Query_Data_Email_Attachment(strworkOrder, strdata, date_from_for_daily_email, date_to_for_daily_email, strHSCODE, strCombination, strProd, strProd_Not, strEmailId);
if (iResult == 1)
{
if (flag == 0)
{
bl_myAccount.insert_last_daily_email_date(date_to_for_daily_email);
flag = 1;
}
strSucessEmail += "" + strEmailId + ", ";
lblMsg.Visible = true;
lblMsg.Text = "Email Sent Succesfully !!!";
continue;
}
else
{
continue;
}
}
}
}
btnLog.Visible = true;
gvAllClients.Visible = true;
inserting_mail_history(ref strSucessEmail, ref strFailedEmail);
}
else
{
lblMsg.Visible = true;
lblMsg.Text = "Please enter a valid date for data mining...";
}
}
catch (Exception ex)
{
}
}
现在,我已经将我的gv绑定在回发ryt之外了...
I have binded my gv outside the postback ryt now Sir...