定义一个类:

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. 海康威视研究院ImageNet2016竞赛经验分享

    原文链接:https://zhuanlan.zhihu.com/p/23249000 目录 场景分类 数据增强 数据增强对最后的识别性能和泛化能力都有着非常重要的作用.我们使用下面这些数据增强方法. ...

  2. break point

    结论: 当有break point时,$m_H(N)=O(N^{k-1})$ bounding function:当break point = k,时成长函数$m_H(N)$的上限这样可以忽略假设集的 ...

  3. Hibernate 配置详解(9)

    hibernate.cache.use_structured_entries Hibernate文档上介绍,该属性是用于把对象以一种更易读的方式放到二级缓存中,这样,在对二级缓存进行监控的时候就更容易 ...

  4. android开发之Animations的使用(二)

    android开发之Animations的使用(二) 本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提 ...

  5. Cocos2dx 中的CCCallFunc,CCCallFuncN,CCCallFuncND,CCCallFuncO比较

    qinning199原创,欢迎转载.转载请注明:http://www.cocos2dx.net/?p=27 首先看一张图片,表示了CCCallFunc的继承关系: 1.CCCallFunc,回调,调用 ...

  6. iOS8 用AVAudioPlayer播放音乐(Swift)

    AVAudioPlayer 类提供了播放音频文件的功能,在本次教程中,我们将对一个音乐文件进行播放暂停和停止操作,此外还会显示标题和播放时间.本次教程使用iOS8和Xcod6.3.1 打开Xcode创 ...

  7. 简单的BFS学习笔记

    什么是BFS传送门. 今天学习BFS,加油! 先定义个数组: struct Node{ int a=0; int b=0; int step=0; }; int map[5][4]={//地图 0,0 ...

  8. 图解MYSQL JOIN ON,SQL JOIN 详解,数据库sql join语句

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  9. jq方法

    DOM属性-获取和设置页面元素的DOM属性 .addClass()..attr()..prop()..hasClass()..html()..removeAttr()..removeClass().. ...

  10. html5的自定义data-*属性与jquery的data()方法的使用

    人们总喜欢往HTML标签上添加自定义属性来存储和操作数据.但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它副 ...