首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >asp列表视图数据源附加

问asp列表视图数据源附加
EN

Stack Overflow用户
提问于 2012-02-16 18:50:00
回答 1查看 952关注 0票数 0

每次刷新页面时,从数据库中获取一组记录的列表视图都会发生变化,例如,如果我有3条记录,并且添加了另一条记录(第4条记录),那么当我刷新页面时,这4条记录似乎被追加到列表视图中!

代码语言:javascript
复制
protected void listComment_Load(object sender, EventArgs e)
    {
        listViewComment.DataSource = Connect.ExecuteSelect(CommandBuilder.GetAllComments());
        listViewComment.DataBind();
    }

我需要先使用某种刷新或清除吗?我在listview加载时调用此方法。

页,ListView控件:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-17 13:07:54

我找到了答案,我所需要做的就是每次调用方法时都创建一个新的!

因此,作为ExecuteSelect方法中的第一行,我们编写了:DataSet dataSet = new DataSet();

如果我们重用包含一些记录的旧DataSet,传入的记录将被RE-添加到adapter.fill(dataSet)调用中,从而在dataSet中显示新旧记录。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9317230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档