Search code examples
c#asp.netvisual-studiogridviewviewstate

ViewState with a List of strings


I am trying to save info from a Gridview on page load to a List of Strings. I then want to take that info and email it out. I have looked online for info on this, and so far I have found nothing. I don't understand if my implementation of the ViewState is wrong or simple in the wrong place. Please help?

protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["messages"] != null)
        {
            messages = (List<string>)ViewState["messages"];
        }

        if (Page.IsPostBack)
        {
            changeByVendor();
            //mailFunction(messages);
        }
        if (!IsPostBack)
        {
            mailFunction(messages);
        }
    }

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((GridView1.DataSourceID == "SqlDataSource2" || GridView1.DataSourceID == "SqlDataSource1") && !(e.Row.Cells[11].Text.Equals("&nbsp;")))
            {
                DateTime myDate = Convert.ToDateTime(e.Row.Cells[11].Text);
                if (DateTime.Now > myDate)
                {
                    e.Row.ForeColor = System.Drawing.Color.Red;
                }
                DateTime myDate2 = Convert.ToDateTime(e.Row.Cells[13].Text);
                if (myDate2 > DateTime.Now && myDate2 < DateTime.Now.AddDays(28))
                {
                    e.Row.Cells[13].BackColor = System.Drawing.Color.Yellow;
                    string thisRow = "";
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        thisRow = thisRow + e.Row.Cells[i];
                    }
                    messages.Add(thisRow);
                }
            }
            else if (GridView1.DataSourceID == "SqlDataSource4" && !(e.Row.Cells[6].Text.Equals("&nbsp;")))
            {
                DateTime myDate = Convert.ToDateTime(e.Row.Cells[6].Text);
                if (DateTime.Now > myDate)
                {
                    e.Row.ForeColor = System.Drawing.Color.Red;
                }
                DateTime myDate2 = Convert.ToDateTime(e.Row.Cells[7].Text);
                if (myDate2 > DateTime.Now && myDate2 < DateTime.Now.AddDays(28))
                {
                    e.Row.Cells[7].BackColor = System.Drawing.Color.Yellow;

                }
            }
            ViewState["messages"] = messages;
        }
    }

Solution

  • Let's suppose we have the following example:

    List<string> input = new List<string>();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["messages"] != null)
        {
            input = (List<string>)ViewState["messages"];
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> msgs = new List<string>() { "1", "2", "3" };
        ViewState["messages"] = msgs;
    }
    

    This will certainly store the List of strings in the viewstate. The problem is, that when we trigger the button click event, the Page_Load event will fire BEFORE the Button1_Click event, thus, the viewstate will still be empty. This can be managed by passing the code into the Page_PreRender event, which will fire after the button_click event.

      protected void Page_PreRender(object sender, EventArgs e)
      {
        if (ViewState["messages"] != null)
        {
            input = (List<string>)ViewState["messages"];
        }
      }