我尝试使用Manually insert an item into a ListView control中的示例。我可以插入一个包含2个字段的项目,但代码会不断为每个项目创建一个新的列表视图。我哪里出错了?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.HtmlControls;
namespace ISEMAILAPP
{
public partial class WebForm2 : System.Web.UI.Page
{
string authorKey = "authors";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// If the session variable is empty, initialize an
// empty list as the datasource
if (Session[authorKey] == null)
{
Session[authorKey] = new List<mEvent>();
}
BindList();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = subject.Text;
string m = message.Text;
// Grab the current list from the session and add the
// currently selected DropDown item to it.
List<mEvent> authors = (List<mEvent>)Session[authorKey];
authors.Add(new mEvent(s,m));
BindList();
}
private void BindList()
{
messagelist.DataSource = (List<mEvent>)Session[authorKey];
messagelist.DataBind();
}
// Basic author object, used for databinding
private class mEvent
{
public String AuthorName { get; set; }
public String Message { get; set; }
public mEvent(string name, string message)
{
AuthorName = name;
Message = message;
}
}
}
}
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js">
</asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js">
</asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js">
</asp:ScriptReference>
</Scripts>
</telerik:RadScriptManager>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" EnableHistory="True" HorizontalAlign="NotSet">
<asp:ListView runat="server" ID="messagelist" >
<ItemTemplate>
<table id="Table1" runat="server">
<tr id="Tr1" runat="server">
<th>Subject</th>
<th>Message</th>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="subjectline" Text='<%# Eval("AuthorName") %>' />
</td>
<td>
<asp:Label runat="server" ID="messageline" Text='<%# Eval("Message") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
Subject: <asp:TextBox ID="subject" runat="server"></asp:TextBox><br /><br />
Message: <asp:TextBox ID="message" runat="server"></asp:TextBox><br /><br />
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
</telerik:RadAjaxPanel>
</form>
</body>
</html>
答案 0 :(得分:0)
添加项目
后,您不会将列表放回会话中protected void Button1_Click(object sender, EventArgs e)
{
string s = subject.Text;
string m = message.Text;
// Grab the current list from the session and add the
// currently selected DropDown item to it.
List<mEvent> authors = (List<mEvent>)Session[authorKey];
authors.Add(new mEvent(s,m));
Session[authorKey] = authors;
BindList();
}