如果用户多次单击按钮,我想多次显示文本。例如,如果我第四次单击按钮,我希望出现四个文本。但是,存在一个问题,阻止它多次出现。
如果用户按三下按钮,我希望它看起来像这样:
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);
}
}
}