Solved

How to keep data stored in generic List<T> available during all processes at the same page.

Posted on 2016-08-13
3
87 Views
Last Modified: 2016-08-20
I need to Save selected items in generic List<T> . and keep it until selecting all items needed. Then by pressing the second button "show search results" .
My problem was when I press the second pagination page number, I lose the selected items in previous page "the List<T> is reset".
Could anyone help me please to keep these selected items accumulated to the List<T> until I finish all items I need to select from all available pages.
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myForm_EE.aspx.cs" Inherits="myGoogleLike_webapp.myForm_EE" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblHead" runat="server" Text=" Enter Your searching test here"></asp:Label>
        <br />
        <asp:TextBox ID="mySearch_TextBox" runat="server"></asp:TextBox>
        <asp:Button ID="search_Button" runat="server" Text="Go" OnClick="search_Button_Click" />
        <br />

        <asp:GridView ID="GridView1" runat="server"  AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="20" AutoGenerateColumns="False" >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Employee ID">
                    <ItemTemplate>
                        <asp:Label ID="lblProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Price" HeaderText="Price" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" Text="show search results"  />
    </div>
    </form>
</body>
</html>

Open in new window


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace myGoogleLike_webapp
{
    public partial class myForm_EE : System.Web.UI.Page
    {
        salesDataContext db = new salesDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void search_Button_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }
        private void BindData()
        {
            string myString = mySearch_TextBox.Text.Trim();
            
            var myProducts = (from mySection in db.Products
                              where mySection.Name.Contains(myString) 
                              select mySection).ToList();

            if ((myProducts != null) && (myProducts.Count() > 0))
            {
                GridView1.DataSource = myProducts;
                GridView1.DataBind();
            }
        }
    }
}

Open in new window

EE_image1.PNGdataContext-diagram.PNG
0
Comment
Question by:Mohammad Alsolaiman
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Paweł earned 500 total points
ID: 41757822
sounds to me like every time you page your'e doing a post-back, loosing everything you've selected you're options are as follows

leverage ajax and store selected items in javascript object between page changes
save the (best, but a pain)

create a viewstate to preserve data between postbacks (good and normal for webforms)

save data to a cookie between postbacks (worst, and most painful)

hope that helps, if i cared i'd go with option one, if this thing just has to work i'd go the view state way

https://msdn.microsoft.com/en-us/library/ms972976.aspx
0
 

Assisted Solution

by:Mohammad Alsolaiman
Mohammad Alsolaiman earned 0 total points
ID: 41763663
thank u Pawel. I had use ViewState. it was very helpful.
namespace myGoogleLike_webapp
{
    // View state example
    public partial class viewStateExample : System.Web.UI.Page
    {
        salesDataContext db = new salesDataContext();
        string mySession = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            mySession = (string)ViewState["mySes"];
            Response.Write((string)ViewState["mySes"]);
        }

        protected void search_Button_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }
        private void BindData()
        {
            string myString = mySearch_TextBox.Text.Trim();

            var myProducts = (from mySection in db.Products
                              where mySection.Name.Contains(myString)
                              select mySection).ToList();

            if ((myProducts != null) && (myProducts.Count() > 0))
            {
                GridView1.DataSource = myProducts;
                GridView1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
        protected void CheckBox1_CheckedChanged(Object sender, EventArgs args)
        {
            foreach (GridViewRow gridViewRow in GridView1.Rows)
            {
                CheckBox chk = (CheckBox)gridViewRow.FindControl("CheckBox1");
                if (chk != null & chk.Checked)
                {
                    string mylblProductID = ((Label)gridViewRow.FindControl("lblProductID")).Text;
                    Boolean isExist = false;
                    if (mySession != null)
                    {
                        isExist = false;
                        string[] words = mySession.Split(' ');
                        foreach (string word in words)
                        {
                            if (words.Contains(mylblProductID))
                            {
                                isExist = true;
                                break;
                            }
                        }
                        chk.Checked = false;
                        if (isExist == false)
                        {
                            mySession += " " + mylblProductID;
                            gridViewRow.BackColor = System.Drawing.Color.Orange;
                        }
                    }
                    else
                    {
                        mySession += " " + mylblProductID;
                        gridViewRow.BackColor = System.Drawing.Color.Orange;
                    }

                }
            }
            ViewState["mySes"] = mySession;
        }
    }
}
0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 41763665
Thank u Pawel very much
1

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now