每当我刷新页面时,listview(它带来了一组来自数据库的记录)正在改变,例如,如果我有3条记录并且我添加了另一条记录(第4条),当我刷新页面时,似乎这4条记录附在列表视图上!
protected void listComment_Load(object sender, EventArgs e)
{
listViewComment.DataSource = Connect.ExecuteSelect(CommandBuilder.GetAllComments());
listViewComment.DataBind();
}
我是否需要先使用某种刷新或清除?我在listview加载上调用此方法。
ASPX页面,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;
}
答案 0 :(得分:1)
我找到了答案,我需要做的就是每次调用方法时创建一个新的DataSet
!
因此,作为ExecuteSelect方法的第一行,我们编写:DataSet dataSet = new DataSet();
如果我们重用旧的dataSet并在其中包含一些记录,那么传入的记录将在adapter.fill(dataSet)
调用中重新添加,从而显示在DataSet中混合的旧记录和新记录。