定义一个类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{    
    [Serializable]
    public class Gushi
    {
        public string Title { get; set; }
        public string Content { get; set; }
    }
}

/.///////////////前台

<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="txtTitle" runat="server" Width="209px"></asp:TextBox>
        <asp:Button ID="btnSave" runat="server" Text="保存" onclick="btnSave_Click" />
        <asp:Button ID="btnQuery" runat="server" Text="查询" onclick="btnQuery_Click" />
        <br />
        <br />
        <asp:TextBox ID="txtContent" runat="server" Height="219px" TextMode="MultiLine" 
            Width="339px"></asp:TextBox>
        <br />
    
    </div>
    </form>
</body>

///////////////////后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebGushi : System.Web.UI.Page
    {
        //List<Gushi> list = new List<Gushi>();
        protected void Page_Load(object sender, EventArgs e)
        {

}

protected void btnSave_Click(object sender, EventArgs e)
        {           
            Gushi gushi = new Gushi();
            gushi.Title = txtTitle.Text;
            gushi.Content = txtContent.Text;
            //ViewState是一个集合,里面可以纺织任何类型的对象,如int,string,以及我们自己定义的类,如Gushi或者List<Gushi>这种特殊类型的对象。
            //判断是不是第一次添加内容,如果是第一次,ViewState中肯定没有任何内容,即为Null;如果不是第一次,则不为Null。
            if (ViewState["gushi"] != null)
            {
                //int i=10;
                //将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;
            }
            txtTitle.Text = string.Empty;
            txtContent.Text = string.Empty;
        }

protected void btnQuery_Click(object sender, EventArgs e)
        {
            txtContent.Text = string.Empty;
            if (ViewState["gushi"] != null)
            {
                List<Gushi> list = ViewState["gushi"] as List<Gushi>;
                //遍历List<Gushi>对象,查询和用户输入的标题相同的古诗,将其内容显示出来
                foreach (Gushi item in list)
                {
                    if (item.Title == txtTitle.Text)
                    {
                        txtContent.Text = item.Content;
                    }
                }
            }          
        }
    }
}

List用法的更多相关文章

  1. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  2. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  3. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  4. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  5. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  6. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  7. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  8. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

  9. 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)

    vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...

  10. [转]thinkphp 模板显示display和assign的用法

    thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...

随机推荐

  1. ceph for openstack快速部署实施

    for el6 即centos6 1,添加ceph 官方yum源 [root@ruiy ~]# cat /etc/yum.repos.d/ceph.repo[ceph-noarch]name=Ceph ...

  2. 深入理解java String 对象的不可变性

    下面我们通过一组图表来解释Java字符串的不可变性 1.声明一个String对象 String s = "abcd"; 2.将一个String变量赋值给另一个String变量 St ...

  3. AndroidUI 布局动画-为列表添加布局动画效果

    新建一个Android project ,使MainActivity 继承自 ListActivity: public class MainActivity extends ListActivity ...

  4. python写xml文件

    为了便于后续的读取处理,这里就将信息保存在xml文件中,想到得到的文件如下: 1 <?xml version="1.0" encoding="utf-8" ...

  5. `npm install --save --save-exact react-native` failed

    当你使用命令行创建一个项目 react-native init  项目名称  后出现以下错误 Installing react-native package from npm... /bin/sh: ...

  6. MVC5 Controller简要创建过程(1):ControllerFactory的创建

    即将离职,闲来无事回顾下MVC的源码,到了Controller创建这里,由于流程有点复杂,鉴于自己记性不太好,索性就记录一下吧,方便日后参照. 首先看MvcHandler: public class ...

  7. context.drawImage绘制图片

    context.drawImage(img,x,y)  x,y图像起始坐标 context.drawImage(img,x,y,w,h) w,h指定图像的宽度和高度 context.drawImage ...

  8. VB.NET函数——数学函数/字母串函数

    一.数学函数 函数 说明 Abs (num) 取绝对值. Exp (num) 返回以e为底.以num为指数的值,如Exp(2)返回e^2值. Log (num) 返回参数num的自然对数值,为Doub ...

  9. 图片压缩上传 Android

    图片压缩的话 想保持 图像清晰度,但是又想保持图片的大小在100k左右. 同时的话又不想自己写那些压缩的代码的话.那你就找对地方了. 提供一个思路. 先读取你的文件,然后读到bitmap里面进行尺寸裁 ...

  10. linux文件名乱码解决办法

    1.linux解压压缩文件乱码 unzip -O CP936 xxx.zip 2.一般文件用convmv sudo convmv -f gbk -t utf-8 -r --notest /your_d ...