I want to display text multiple times if the user clicked the button multiple times. Like for example, if I clicked the button fourth time, I want four text to appear. However, there is an issue going on that prevents it from appearing multiple times.
I was expecting it to appear like this if the user press the button three times:
Hello world
Hello world
Hello world
But it showed me this:
Hello world
Is there anyone can help me with this problem....
Here's the source code:
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);
}
}
}