欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

ViewState的使用

发布时间:2025/7/14 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 ViewState的使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

前台部分:布局如图所示: 

<div>
   
        请输入标题:<asp:TextBox ID="TextBox1" runat="server" Width="178px"></asp:TextBox>
        <br />
        请输入内容:<asp:TextBox ID="TextBox2" runat="server" Height="131px"
            TextMode="MultiLine" Width="180px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnsave" runat="server" Text="保存" οnclick="btnsave_Click" />
        <asp:Button ID="btnselect" runat="server" Text="查询" οnclick="btnselect_Click" />
   
    </div>

------------------后台部分

   protected void btnsave_Click(object sender, EventArgs e)
        {
            Gushi gushi = new Gushi();
            gushi.Title = TextBox1.Text;
            gushi.Content = TextBox2.Text;
            if (ViewState["gushi"] != null)
            {
                //将ViewStae中存储的List<Gushi>对象取出来
                List<Gushi> list = ViewState["gushi"] as List<Gushi>;
                //将新的内容添加到List<Gushi>中
                list.Add(gushi);
                ViewState["gushi"] = list;
            }
            else
            {
                List<Gushi> list = new List<Gushi>();
                list.Add(gushi);
                ViewState["gushi"] = list;
            }
            TextBox1.Text = string.Empty;
            TextBox2.Text = string.Empty;
        }

        protected void btnselect_Click(object sender, EventArgs e)
        {
            TextBox2.Text = string.Empty;
            if(ViewState["gushi"]!=null)
            {
            List<Gushi> list=ViewState["gushi"] as List<Gushi>;
            foreach (Gushi item in list)
            {
                if(item.Title==TextBox1.Text)
                {
                    TextBox2.Text = item.Content;
                }
            }
            }
        }

程序运行后,我在标框看中输入php,下面也输入内容,然后输入net,同样输入内容,然后再回来查询php,

查询结果如图所示:

转载于:https://www.cnblogs.com/qzc900809/archive/2013/04/02/2996693.html

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的ViewState的使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。