开发者

How can I use the selected rows in GridView as a source for a GridView in another page?

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 contai开发者_Python百科ned 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 the user is able to know which items he has selected?

I tried with a for loop on the FirstPage:

   protected void Page_Load(object sender, EventArgs e)
    {
        List<int> ids = new List<int>();

         if (!IsPostBack)
        {

         }
        else
         {
             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)
                 {
                     ids.Add(bookID);
                 }
             }

             Session["Ids"] = ids;

             Response.Redirect("SecondPage.aspx"); 
         }
    }

and on the SecondPage:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dtBooks = new DataTable("Books");
    dtBooks.Columns.Add(new DataColumn("ID", typeof(int)));

    if (!IsPostBack)
    {
        var list = (List<int>)Session["Ids"];

        foreach (int id in list)
        {
            if (Request.QueryString["bookID" + id] != null)
            {
                DataRow row;
                row = dtBooks.NewRow();
                row["ID"] = Request.QueryString["bookID" + id];
                dtBooks.Rows.Add(row);
            }
        }

        GridView1.DataSource = dtBooks;
        GridView1.DataBind();

    }
    else
    { 
    }
}

but I get no GridView table on the second page. I would be very grateful if anyone notices my mistake and points it out. Hope you can help me.


This is a common issue when setting session variables before a redirect. I think you can work around it by using the overloaded Response.Redirect method:

Response.Redirect("...", false); // false = don't stop execution

See here for more details:
Session variables lost after Response.Redirect

Another option is to store the IDs in a hidden field, and access them with Page.PreviousPage, like this:

HiddenField hidden = (HiddenField)Page.PreviousPage.FindControl("MyHiddenField");    
string values = hidden.Value; 

Lastly, depending on what the page is doing, you might want to use Server.Transfer here. There are drawbacks to this approach, but there are situations where it's applicable.


In your second page, you are checking for a query string variable before adding a row to dtBooks:

if (Request.QueryString["bookID" + id] != null)

However, you are not passing any query strings when you redirect:

Response.Redirect("SecondPage.aspx");

At a guess, I would think that you originally tried using the query string to pass the IDs, before changing to the session and you haven't updated all of your code.

I am somewhat concerned about your first page code, though. You do realize that you will redirect to the second page whenever a post back occurs? That means that no matter what buttons / controls you have on the first page, if they post back for any reason, you will redirect to the second page.

EDIT AFTER COMMENTS

If you aren't using the query string, then don't use the query string:

foreach (int id in list)
{
    DataRow row;
    row = dtBooks.NewRow();
    row["ID"] = id;
    dtBooks.Rows.Add(row);
}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜