【问题标题】:How to create ASP.NET labels multiple times by clicking a button multiple times如何通过多次单击按钮多次创建 ASP.NET 标签
【发布时间】:2013-11-10 08:39:24
【问题描述】:

如果用户多次单击按钮,我想多次显示文本。例如,如果我第四次单击按钮,我希望出现四个文本。但是,存在一个问题,导致它无法多次出现。

如果用户按三下按钮,我希望它会显示为这样:

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);
        }
    }
}

【问题讨论】:

  • 使用 lbl_homeCarouselAdd.Text += strDiv.ToString();
  • 试过了,还是一样
  • 我已经用解决方案 2 编辑了我的答案,请检查一下。

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


【解决方案1】:

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

替换这个:

lbl_homeCarouselAdd.Text = strDiv.ToString();

以下:

lbl_homeCarouselAdd.Text += strDiv.ToString();

解决方案 2:

您不需要每次都创建标签,因此将标签声明移至函数外部。

解决方案 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);
        }
    }
}

【讨论】:

  • 试过了,结果一样
  • 我在我的答案中添加了解决方案3,请立即查看。
【解决方案2】:

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

请参阅 herehere

【讨论】:

  • 试过了,还是一样。
  • Hello world

    ,
    为什么在标签之后需要
    标签?
  • yes
    标签需要在每个标签之后创建新行,否则所有标签将显示在同一行/行中
  • 我认为当您单击该按钮时,您会发回服务器端,.Text 字段将被删除。尝试将输出保存在类中的字符串变量中,并在用户每次单击按钮时将其提供给.Text
  • 是的,你是对的,因为每个回发标签的内容都是空的。我添加了静态 stringbuilder 对象来保存标签的旧数据。还有其他最好的选择吗?感谢您的宝贵意见。
猜你喜欢
  • 2022-11-14
  • 2018-08-15
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
相关资源
最近更新 更多