List用法

本文介绍了一个使用ASP.NET进行古诗保存与查询的应用实例。通过编写前后台代码,演示了如何利用ViewState特性在服务器端存储和读取用户输入的数据。具体实现了古诗标题和内容的保存及根据标题查询对应内容的功能。

定义一个类:

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

转载于:https://www.cnblogs.com/yzc19838458/p/3167032.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值