从不同的表单返回后保持GridView的状态

时间:2017-07-06 21:51:53

标签: c# list gridview viewstate page-load-time

我有以下课程:

        [Serializable]
        public class ClientBLL
        {
        private int _Rut;
        private String _Fullname;
        private List<CreditCardBLL> _CreditCards;

        public ClientBLL(){}

        public int Rut
        {
            get { return _Rut; }
            set { _Rut = value; }
        }

        public String Fullname
        {
            get { return _Fullname; }
            set { _Fullname = value; }
        }

        public List<CreditCardBLL> CreditCards
        {
            get { return _CreditCards; }
            set { _CreditCards = value; }
        } 



        public Boolean Create()
        {
            Boolean exit = false;

            try
            {
                using (DBEntities context = new DBEntities())
                {
                    var clientDAL = context.Client.FirstOrDefault(client 
                    => client.Rut == Rut);

                    if (clientDAL == null)
                    {
                        clientDAL = new Client();

                        clientDAL.Rut = Rut;
                        clientDAL.Fullname = Fullname;

                        context.Client.AddObject(clientDAL);

                        exit = context.SaveChanges() > 0;
                    }
                    else
                    {
                        MessageBox.Show("Client cannot be created! This 
                        Client already exists!");
                    }

                    return exit;
                }
            }
            catch (Exception ex)
            {
              MessageBox.Show("Exception (C, BLL): " + ex.Message);
            }

            return exit;
        }

        public void AddCreditCard(CreditCardBLL newCreditCard)
        {
            newCreditCard.Create(Rut);
            CreditCards.Add(newCreditCard);
        }
      }

        [Serializable]
        public class CreditCardBLL
    {
        private int _Number;
        private int _Code;
        private DateTime _ValidDate;

        public CreditCardBLL(){}

        public int Number
        {
            get { return _Number; }
            set { _Number = value; }
        }

        public int Code
        {
            get { return _Code; }
            set { _Code = value; }
        }

        public DateTime ValidDate
        {
            get { return _ValidDate; }
            set { _ValidDate = value; }
        }

        public Boolean Create(int ClientRut)
        {
            Boolean exit = false;

            try
            {
                using (DBEntities context = new DBEntities())
                {
                    var creditCardDAL = 
                    context.CreditCard.FirstOrDefault(card => card.Number 
                    == Number);

                    if (creditCardDAL == null)
                    {
                        creditCardDAL = new CreditCard();

                        creditCardDAL.Number = Number;
                        creditCardDAL.Code = Code;
                        creditCardDAL.ValidDate = ValidDate;
                        creditCardDAL.Client = ClientRut;

                        context.CreditCard.AddObject(creditCardDAL);

                        exit = context.SaveChanges() > 0;
                    }
                    else
                    {
                        MessageBox.Show("Credit Card cannot be created! 
                        This Credit Card already exists!");
                    }

                    return exit;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("Exception (CC, BLL): " + ex.Message);
            }

            return exit;
             }
             }

代码隐藏:

Clients.aspx.cs

        public partial class Registration : System.Web.UI.Page
        {

        private List<ClientBLL> clients;
        private List<int> pageLoads;

    protected void Page_Load(object sender, EventArgs e)
    {

        int number = Convert.ToInt32(ViewState["Number"]);
        number = number + 1;
        ViewState["Number"] = number;

        MessageBox.Show("Page_Load: " + number);

        pageLoads = (List<int>)ViewState["Index"];



        if (pageLoads == null)
        {
            pageLoads = new List<int>();
        }
        else
        {
            MessageBox.Show("PageLoads != null");
        }

        pageLoads.Add(number);

        MessageBox.Show("PageLoads.Count: "+pageLoads.Count);


        ViewState["Index"] = pageLoads;


        for (int i = 0; i < pageLoads.Count; i++)
        {

            int index = pageLoads.FindIndex(value => value == number);

            if (index == 3)
            {

                FillGridView();
            }


            MessageBox.Show("Index: " + index);
        }

        if (!IsPostBack)
        {
            gvClients.DataSource = new object[] { null };
            gvClients.DataBind();
        }
        else
        {
            clients = (List<ClientBLL>)ViewState["Clients"];

            if (clients == null)
            {
                clients = new List<ClientBLL>();
            }

        }

    } 

            protected void gvClients_RowCommand(object sender, 
            GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btnAdd")
        {
            try
            {
                ClientBLL newClient = new ClientBLL();

                String strRut = ((TextBox)gvClients.FooterRow.FindControl("txtRutInsert")).Text;
                int intRut = int.Parse(strRut);
                newClient.Rut = intRut;

                String fullname = ((TextBox)gvClients.FooterRow.FindControl("txtFullnameInsert")).Text;
                newClient.Fullname = fullname;

                newClient.Create();

                clients.Add(newClient);

                ViewState["Clients"] = clients;

                FillGridView();

            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("NullReferenceException (C, UI): " + ex.Message);
            }
            catch (FormatException ex)
            {
                MessageBox.Show("FormatException (C, UI): " + ex.Message);
            }

        }
        else if (e.CommandName == "btnCards")
        {

            var rowIndex = Convert.ToInt32(e.CommandArgument);

            Session["Client"] = clients[rowIndex];

            String clientID;
            clientID = Session.SessionID;

            MessageBox.Show("Client Session ID (Client): " + clientID);



            Response.Redirect("CreditCards.aspx");

        }
        else if (e.CommandName == "btnDelete")
        {

        }

      }

    protected Boolean FillGridView()
    {
        Boolean flag = false;

        if (clients == null)
        {
            MessageBox.Show("Clients is empty! Can't bind to it!");
        }
        else
        {
            gvClients.DataSource = clients;
            gvClients.DataBind();


            flag = true;
        }

        return flag;
    }
   }

CreditCards.aspx.cs:

    public partial class Cards : System.Web.UI.Page
    {

    private ClientBLL client;


    protected void Page_Load(object sender, EventArgs e)
    {





        if (!IsPostBack)
        {
            if (Session["Client"] != null)
            {
                MessageBox.Show("Session Client != null (!IsPostBack)");
            }
            else
            {
                MessageBox.Show("Session Client == null (!IsPostBack)");
            }

            gvCreditCards.DataSource = new object[] { null };
            gvCreditCards.DataBind();


        }
        else
        {
            client = (ClientBLL)Session["Client"];

            //form1 = (HtmlForm) ViewState["Page"];



            if (client.CreditCards == null)
            {
                client.CreditCards = new List<CreditCardBLL>();
            }
        }


     }

    protected void gvCreditCards_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btnAdd")
        {
            try
            {
                CreditCardBLL newCreditCard = new CreditCardBLL();

                String strNumber = ((TextBox)gvCreditCards.FooterRow.FindControl("txtNumberInsert")).Text;
                int intNumber = int.Parse(strNumber);
                newCreditCard.Number = intNumber;

                String strCode = ((TextBox)gvCreditCards.FooterRow.FindControl("txtCodeInsert")).Text;
                int intCode = int.Parse(strCode);
                newCreditCard.Code = intCode;

                String strValidDate = ((TextBox)gvCreditCards.FooterRow.FindControl("txtValidDateInsert")).Text;
                DateTime datValidDate = DateTime.Parse(strValidDate);
                newCreditCard.ValidDate = datValidDate;


                client.AddCreditCard(newCreditCard);

                String clientID;
                clientID = Session.SessionID;

                MessageBox.Show("Client Session ID (CreditCard): " + clientID);

                FillGridView();

            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("NullReferenceException (CC, UI): " + ex.Message);
            }
            catch (FormatException ex)
            {
                MessageBox.Show("FormatException (CC, UI): " + ex.Message);
            }
        }
    }

    protected Boolean FillGridView()
    {
        Boolean flag = false;

        if (client.CreditCards == null)
        {
            MessageBox.Show("CreditCards is empty! Can't bind to it!");
        }
        else
        {
            gvCreditCards.DataSource = client.CreditCards;
            gvCreditCards.DataBind();



            flag = true;
        }

        return flag;
    }

    protected void btnBackToClients_Click(object sender, EventArgs e)
    {

        if (Session["Client"] != null)
        {
            MessageBox.Show("Client's Session != null (Back to Clients)");
        }
        else
        {
            MessageBox.Show("Client's Session == null (Back to Clients)");
        }

        String rut, number;

        rut = client.Rut.ToString();
        number = client.CreditCards[0].Number.ToString();

        MessageBox.Show("Client's rut: " + rut + " CreditCard's number: " + number);


        String clientID;
        clientID = Session.SessionID;

        MessageBox.Show("Client Session ID (CreditCard) after closing the current one: " + clientID);

        //Session testing
        Boolean testing = true;

        Session["Test"] = testing;

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

嗯,情况是在Clients.aspx.cs中我有一个GridView中的客户端列表,每个客户端都有自己的CreditCards列表。当我点击添加特定客户端的新CreditCards时,它将重定向到CreditCards.aspx.cs,而在添加了whisnk CreditCards之后,它在之前点击的当前客户的CreditCards的GridView中有一个列表。这是棘手的部分。每次我想回到Clients.aspx.cs时,客户端列表(GridView)都消失了,我需要保持活着才能管理客户端。另一方面,需要根据点击的客户端显示CreditCards(GridView)列表。

我已经尝试了很多,但我无法弄清楚!!!

非常感谢!!!

0 个答案:

没有答案