1

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);
        }
    }
}
Minelava
  • 221
  • 1
  • 7
  • 21

2 Answers2

2

Solution 1: You are just replacing the new label with old one but not adding to existing labels that's why you are not able to see the multiple labels even when you clicked a button multiple times.

Replace this:

lbl_homeCarouselAdd.Text = strDiv.ToString();

With following:

lbl_homeCarouselAdd.Text += strDiv.ToString();

Solution 2:

You don't need to create a Label everytime so move your Label Declaration to outside the function.

Solution 3:

When you postback the page to your server each time all dynamically created controls will be removed(here in your case your labels will be removed). Hence you should maintain the label data into some variable to retain it for next usage. here i have used StringBuilder to save the old label data. for better approach please see the link Dynamically Created Controls Loosing Data

modified code should be as below:

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);
        }
    }
}
Community
  • 1
  • 1
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

For every postback that you do clicking the button, the value of .Text field is erased, you can try keeping the value in static string variables, for more solutions and details see below links.

See here and here.

Vahid Nateghi
  • 576
  • 5
  • 14
  • Tried that but it's the same. – Minelava Nov 10 '13 at 08:46
  • Hello world,
    why do we need
    tag after tag?
    – Miller Nov 10 '13 at 08:47
  • yes
    tag is required further to create new line after every label otherwise all labels will be displayed in same line/row
    – Sudhakar Tillapudi Nov 10 '13 at 08:53
  • I think when you click the button you post back to your server side and the `.Text` field is erased. Try keeping your output in a string variable in you class and giving it to the `.Text` each time the user clicks on the button. – Vahid Nateghi Nov 10 '13 at 08:54
  • 1
    yes Vahid you are right for every postback labels content will be emepty. i have added static stringbuilder object to hold the old data of labels. is there any other best alternative to this? thanks for your valuable point. – Sudhakar Tillapudi Nov 10 '13 at 09:14
  • @Sudhakar: You're welcome. I added two links to my answer, see if they can be of any help. – Vahid Nateghi Nov 10 '13 at 09:30
  • @VahidNateghi : it is really intresting Thanks, but i think all dynamically created controls will be removed for every Postback request am i right? – Sudhakar Tillapudi Nov 10 '13 at 09:33
  • @Sudhakar: Did a little search, yes you're right. Every dynamically created component will not survive the postback (i.e. Buttons, textboxes, etc). – Vahid Nateghi Nov 10 '13 at 09:39
  • @VahidNateghi:Thank you. i have updated my answer with the same and alos provided link which can help us to how to retain the dynamically created controls when page is PostedBack. – Sudhakar Tillapudi Nov 10 '13 at 09:45