ASP.NET AJAX Toolkit不断将新内容添加到旧内容中

时间:2013-12-09 02:58:54

标签: c# asp.net ajax

我有这个ASP.NET AJAX工具包。每次触发AJAX时,我都会获得旧数据+新数据,并且它会不断添加和添加(因为我后面的代码中有+ =)如何解决这个问题,以便AJAX只显示更新后的版本,而不是不断添加?

ASPX:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="1000"></asp:Timer>
            <ul id="myaspajaxtoolkit" runat="server"></ul>
    </ContentTemplate>
</asp:UpdatePanel>
代码背后的代码:

SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");

protected void Page_Load(object sender, EventArgs e)
{
    cnn.Open();
    SqlCommand cmd = new SqlCommand("SELECT EmployeeName FROM Employees", cnn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
        {
            while (dr.Read())
            {
                myaspajaxtoolkit.InnerHtml += "<li>" + dr.GetString(0) + "</li>";
            }
        }
    cnn.Close();
}

1 个答案:

答案 0 :(得分:2)

string html = string.Empty;
if (dr.HasRows)
{
    while (dr.Read())
    {
         html+= "<li>" + dr.GetString(0) + "</li>";
    }
}
myaspajaxtoolkit.InnerHtml = html;