I am writing a web site in Visual Studio, something like an on-line library. I have a GridView on the first page that presents all of the books available from the data source and some other properties also contained in the data source. The GridView contains check boxes and the user can choose which books he wants to order by checking a box. My question is how can I use the data in the selected rows, the list of books with their properties and show that list on another page, so that开发者_StackOverflow中文版 the user is able to know which items he has selected?
I tried with a for loop on the FirstPage:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int bookID = (int)GridView1.DataKeys[i][0];
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (cb.Checked)
{
purchaseProductList.Add(bookID);
Response.Redirect("SecondPage.aspx?bookID" + i + "=" + bookID);
}
}
and then on the SecondPage:
for (int i = 0; i < 10; i++)
{
if (Request.QueryString["bookID" + i] != null)
{
DataRow row;
row = dtBooks.NewRow();
row["ID"] = Request.QueryString["bookID" + i];
dtBooks.Rows.Add(row);
}
}
GridView1.DataSource = dtBooks;
GridView1.DataBind();
but it didn't work. Any help? Thank you in advance.
you can use session to keep selected ids
List<string> ids = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int bookID = (int)GridView1.DataKeys[i][0];
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (cb.Checked)
{
purchaseProductList.Add(bookID);
ids.Add(bookID);
}
}
Session["Ids"] = ids;
Response.Redirect("SecondPage.aspx");
from second page you can access session and load those ids to grid
var list = (List<string>)Session["Ids"];
foreach (string id in list)
{
DataRow row;
row = dtBooks.NewRow();
row["ID"] = Request.QueryString["bookID" + id];
dtBooks.Rows.Add(row);
}
GridView1.DataSource = dtBooks;
GridView1.DataBind();
精彩评论