如何通过多次单击按钮多次创建ASP.NET标签

时间:2013-11-10 08:39:24

标签: c# asp.net visual-studio-2012 webforms

如果用户多次点击按钮,我想多次显示文字。例如,如果我第四次单击该按钮,我想要显示四个文本。但是,有一个问题会阻止它多次出现。

如果用户按下按钮三次,我希望它看起来像这样:

Hello world
Hello world
Hello world

但它告诉我这个:

Hello world

有没有人可以帮我解决这个问题....

这是源代码:

WebForm1.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"  Inherits="addlabels.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server"></asp:Panel>
        <asp:Button ID="add" runat="server" Text="Add more" OnClick="add_click"/>

    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Diagnostics;
using System.Text;

namespace addlabels
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        int pressNumberOfTimes;

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void add_click(object sender, EventArgs e)
        {
            // Add the panel
            pressNumberOfTimes++;

            Label lbl_homeCarouselAdd = new Label();

            // Set the label's Text and ID properties.
            lbl_homeCarouselAdd.ID = "lbl_homeCarouselAdd" + pressNumberOfTimes;

            StringBuilder strDiv = new StringBuilder();
            strDiv.Append(string.Format(@"<p class='style'>Hello world</p>"));

            lbl_homeCarouselAdd.Text = strDiv.ToString();


            Panel1.Controls.Add(lbl_homeCarouselAdd);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

解决方案1: 您只是将新标签替换为旧标签,但不添加到现有标签,这就是即使多次单击按钮也无法看到多个标签的原因。

替换为:

lbl_homeCarouselAdd.Text = strDiv.ToString();

以下内容:

lbl_homeCarouselAdd.Text += strDiv.ToString();

解决方案2:

您不需要每次都创建一个Label,因此请将Label声明移到函数外部。

解决方案3:

每当所有动态创建的控件都被删除时(在这种情况下,您的标签将被删除),当您将页面回发到服务器时。因此,您应该将标签数据保存到某个变量中以保留它以供下次使用。在这里,我使用StringBuilder来保存旧标签数据。 有关更好的方法,请参阅链接Dynamically Created Controls Loosing Data

修改后的代码应如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Diagnostics;
using System.Text;

namespace addlabels
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        int pressNumberOfTimes;
        Label lbl_homeCarouselAdd = new Label();
        static StringBuilder strDiv = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void add_click(object sender, EventArgs e)
        {
            // Add the panel
            pressNumberOfTimes++;



            // Set the label's Text and ID properties.
            lbl_homeCarouselAdd.ID = "lbl_homeCarouselAdd" + pressNumberOfTimes;


            strDiv.Append(string.Format(@"<p class='style'>Hello world</p>"));

            lbl_homeCarouselAdd.Text += strDiv.ToString();


            Panel1.Controls.Add(lbl_homeCarouselAdd);
        }
    }
}

答案 1 :(得分:1)

对于您单击按钮的每个回发,.Text字段的值都将被删除,您可以尝试将值保留在静态字符串变量中,以获取更多解决方案和详细信息,请参见下文链接。

请参阅herehere