[c#]视图状态的字符串列表

发布时间: 2017/2/25 1:04:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图保存信息从 Gridview,在页面加载到字符串列表。然后,我想要采取的信息和电子邮件了。我看过在线的信息,和到目前为止有什么发现了。我不明白如果我执行视图状态错误或简单在错误的地方。请帮助吗?

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;
        }
    }

解决方法 1:

让我们假设我们有下面的示例︰

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;
}

当然,这将在视图状态中存储字符串的列表。问题在于,当我们触发按钮的单击事件,Page_Load 事件就会触发Button1_Click 事件,因此,视图状态将仍然为空。这可以通过将代码传递到该位置的事件,将会触发 button_click 事件之后管理。

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