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;
}
}
}
}
}
}
List用法的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
- [转]thinkphp 模板显示display和assign的用法
thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...
随机推荐
- 帮助中心 7D-我的私家设计师 设计师品牌服饰集成网 7D服装定制!
帮助中心 7D-我的私家设计师 设计师品牌服饰集成网 7D服装定制! 关于我们
- instance variables may not be placed in categories
Avoid Properties in Categories Objective-C分类中是不允许增加成员变量的(Instance variables may not be placed in cat ...
- Cube(规律)
Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- extern外部方法使用C#简单样例
外部方法使用C#简单样例 1.添加引用using System.Runtime.InteropServices; 2.声明和实现的连接[DllImport("kernel32", ...
- django中怎样生成非HTML格式的内容。
某些时候可能有这种需求.在网页中点击一个链接或者一个button希望返回一张图片.一个pdf文档.一个csv文档等而非HTML. 在diango中非常easy做到这些.django中的view用来接收 ...
- poj2762 Going from u to v or from v to u?
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13040 ...
- Sass介绍及入门教程
Sass是什么? Sass是"Syntactically Awesome StyleSheets"的简称.那么他是什么?其实没有必要太过于纠结,只要知道他是“CSS预处理器”中的一 ...
- 温故而知新之数据库的分离和附加…高手请跳过….
sql server2005分离数据库后,把路径下的两个文件拷到自己想要存放的目录下,然后再附加
- SharePoint 2013设置“以其他用户身份登录”
登录web服务器,打开位于“C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\CONTR ...
- CRM odata方法 js容易出现的错误,大小写区分 Value Id
Id Value 注意大小写,I大写,V大写,typeResults.result[0].yt_category.Value; 否则会报 错,Result.yt_businessunit_terri ...