每次刷新页面时,从数据库中获取一组记录的列表视图都会发生变化,例如,如果我有3条记录,并且添加了另一条记录(第4条记录),那么当我刷新页面时,这4条记录似乎被追加到列表视图中!
protected void listComment_Load(object sender, EventArgs e)
{
listViewComment.DataSource = Connect.ExecuteSelect(CommandBuilder.GetAllComments());
listViewComment.DataBind();
}我需要先使用某种刷新或清除吗?我在listview加载时调用此方法。
页,ListView控件:
<asp:ListView ID="listViewComment" runat="server" OnLoad="listComment_Load" class="container-commentBox">
<LayoutTemplate>
<h1>
Shout Box
</h1>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("username") %>:
<br />
<%# Eval("comment") %>
</div>
</ItemTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>ExecuteSelect:
public static DataSet ExecuteSelect(string query)
{
try
{
connection.Open();
command.CommandText = query;
command.Connection = connection;
adapter.SelectCommand = command;
using (adapter)
{
adapter.Fill(dataSet);
}
}
catch (Exception e)
{
}
finally
{
connection.Close();
}
return dataSet;
}发布于 2012-02-17 13:07:54
我找到了答案,我所需要做的就是每次调用方法时都创建一个新的!
因此,作为ExecuteSelect方法中的第一行,我们编写了:DataSet dataSet = new DataSet();
如果我们重用包含一些记录的旧DataSet,传入的记录将被RE-添加到adapter.fill(dataSet)调用中,从而在dataSet中显示新旧记录。
https://stackoverflow.com/questions/9317230
复制相似问题