• List用法


    定义一个类:

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

  • 相关阅读:
    SMR磁盘学习1
    练笔-字符串,向量和数组6
    练笔-字符串,向量和数组5
    练笔-字符串,向量和数组4
    练笔-字符串,向量和数组3
    练笔--字符串,向量和数组2
    Redis系列-存储篇hash主要操作函数小结
    Redis系列-存储篇string主要操作函数小结
    Redis系列-远程连接redis并给redis加锁
    Pjax的使用
  • 原文地址:https://www.cnblogs.com/yzc19838458/p/3167032.html
Copyright © 2020-2023  润新知