ViewState的使用

it2022-05-21  65

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

<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

相关资源:数据结构—成绩单生成器

最新回复(0)