You must Sign In to post a response.
  • Category: ASP.NET

    Update label message

    Hi,
    i want to change label message in loop. but its not effecting. it will effect in final step.
    please suggest me how to update label message using asp.net weballication.

    protected void Button1_Click(object sender, EventArgs e)
    {
    for (int i = 1; i <= 10; i++)
    {
    Label1.Text = i.ToString();
    }
    }

    thanks
  • #762750
    Hello Thyagarajulu,

    Refer the updated code :

    private void button1_Click(object sender, EventArgs e)
    {
    Label1.Text = "";
    for (int i = 1; i <= 10; i++)
    {
    Label1.Text += i.ToString() + Environment.NewLine;
    }
    }


    See the attached image. it's an output of this code.


    Hope this is the exact output that you are looking for.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762751
    Hi Nirav,

    I dont want to display all 1 to 10 numbers
    like if firs loop running need to display label,text is 1 ....
    second loop label.text is 2 .......

  • #762753
    Hello Thyagarajulu,

    I am not getting what you want to do exactly.

    there may be two possibilities about that you are asking.

    either you can do like this on button click.:

    int j = 1;
    Label1.Text = "";


    private void button1_Click(object sender, EventArgs e)
    {
    for (i = j; i <= 10; i++)
    {
    Label1.Text += i.ToString() + Environment.NewLine;
    break;
    }
    }

    You must have to click on button for 10 times.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762754
    Hi,

    make a console application and write over there instead of display the same in label, As of my knowledge you can't achieve that using label.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762757
    Hi Reddy.

    Add scriptmanager, update panel, timer control and a label control on the wepage.
    Set time interval of timer control to 1000. This will make your label display for every one second.
    Starts from 0,1,2,3.... till the value you specify.


    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" ClientIDMode="Inherit" runat="server">
    <ContentTemplate>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
    </asp:Timer>
    </ContentTemplate>
    </asp:UpdatePanel>
    </form>

    In the code behind file write below code

    public void display()
    {
    if (ViewState["lbl"] == null)
    {
    Label1.Text = Convert.ToString(0);
    ViewState["lbl"] = 0;
    }
    else
    {
    int i = Convert.ToInt32( ViewState["lbl"]);
    if (i == 10)
    {
    Label1.Text = Convert.ToString(10);
    }
    else
    {
    i = i + 1;
    ViewState["lbl"] = i.ToString();
    Label1.Text = i.ToString();
    }
    }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
    display();
    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #762769
    Hai Thyagarajulu Reddy,
    Actually your code is fine and working as expected. The problem is it's getting the value and then replacing it with the next value and so on. So once it get the last loop, the value is remains as it is not replaced.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #762774
    Hi Pawan.

    His requirement seems be like he want to show 0 to 10 digits as label text one by one changing. That changes he should be able to see after he run his code.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762778
    Hello Sridhar,

    I am not sure what he want to do exactly. He said that want to change the value after i's value is increased. But don't know on which event and how he want to change the value.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments