【问题标题】:ViewState with a List of strings带有字符串列表的 ViewState
【发布时间】:2017-03-09 02:31:24
【问题描述】:

我正在尝试将页面加载时来自 Gridview 的信息保存到字符串列表。然后,我想获取该信息并通过电子邮件发送出去。我在网上搜索了这方面的信息,到目前为止我什么也没找到。我不明白我对 ViewState 的实现是错误的还是在错误的地方简单。请帮忙?

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

【问题讨论】:

  • 你有多个信息吗?在ViewState 中,您是否正在重定向它?你能详细说明你在做什么吗?
  • 你为什么在你的 OnDataBound 事件if ((GridView1.DataSourceID == "SqlDataSource2" || GridView1.DataSourceID == "SqlDataSource1") 中检查它,它要么是要么不是两者,所以如果你不重新绑定它或者它不为空,那么这不是必需的 message 的值是什么当你单步执行代码时..?

标签: c# asp.net visual-studio gridview viewstate


【解决方案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 事件将触发 BEFORE Button1_Click 事件,因此,视图状态仍然为空。这可以通过将代码传递给 Page_PreRender 事件来管理,该事件将在 button_click 事件之后触发。

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

【讨论】:

  • 我厌倦了它,它并没有改变任何东西。我必须以某种方式错误地存储信息。也许我会从 GridView OnRow 事件中更改我的实现
猜你喜欢
  • 2019-02-23
  • 1970-01-01
  • 2018-09-07
  • 2013-10-21
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
相关资源
最近更新 更多