locked
asp.net append text in panel RRS feed

  • Question

  • User-418973555 posted

    trying to append label / text to second line in panel, but it only shows one line thats it

    protected void btnSubmit_Click1(object sender, EventArgs e)
      {
       
    
        Session["cCounter"] = Convert.ToInt64(Session["cCounter"]) + 1;
    
        Literal ltbr = new Literal();
        ltbr.ID = "ltbr" + Session["cCounter"].ToString();
        ltbr.Text = "</br>";
    
        Label lblnewline = new Label();
        lblnewline.ID = "lblnewline" + Session["cCounter"].ToString();
        lblnewline.Text = "newline";
    
        pnlChatArea.Controls.Add(ltbr);
        pnlChatArea.Controls.Add(lblnewline);
    
        
    
      }

    Thursday, June 27, 2019 4:05 AM

Answers

  • User665608656 posted

    Hi larnvok,

    According to your description, the reason why you still have only one 'new line' from the second click is that the button refreshes the page, causing the controls that were added to the panel to be refreshed.

    Therefore, I suggest that you store the current panel in the session after each panel content addition, and determine whether there is content in the session before adding controls to the panel.

    If there is, loop the controls properties of the panel in the session.

    Put all the original controls in the panel to store in a list first, and then loop this list and add the control to the current panel.

    Finally, the subsequent add action is executed.

    For more details, you could refer to the following code:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Session["cCounter"] = 0;
                    Session["panelcontent"] = null;
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["cCounter"] = Convert.ToInt64(Session["cCounter"]) + 1;
                Literal ltbr = new Literal();
                ltbr.ID = "ltbr" + Session["cCounter"].ToString();
                ltbr.Text = "</br>";
    
                Label lblnewline = new Label();
                lblnewline.ID = "lblnewline" + Session["cCounter"].ToString();
                lblnewline.Text = "newline";
    
                if (Session["panelcontent"] != null)
                {
                    Panel panel1 = Session["panelcontent"] as Panel;
                    List<Control> controls = new List<Control>();
                    foreach (Control item in panel1.Controls)
                    {
                        controls.Add(item);
                    }
                    for (int i = 0; i < controls.Count; i++)
                    {
                        pnlChatArea.Controls.Add(controls[i]);
                    }
                }
                pnlChatArea.Controls.Add(ltbr);
                pnlChatArea.Controls.Add(lblnewline);
    
                Session["panelcontent"] = pnlChatArea;
            }

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2019 8:17 AM

All replies

  • User-1038772411 posted

    Hello larnvok09,

    As you want to append label / text to second line in panel.

    Try with this 

    protected void btnSubmit_Click1(object sender, EventArgs e)
      {
       
    
        Session["cCounter"] = Convert.ToInt64(Session["cCounter"]) + 1;
    
        Literal ltbr = new Literal();
        ltbr.ID = "ltbr" + Session["cCounter"].ToString();
        ltbr.Text = "</br>";
    
        Label lblnewline = new Label();
        lblnewline.ID = "lblnewline" + Session["cCounter"].ToString();
        lblnewline.Text = "newline";
    
        pnlChatArea.Controls.Add(ltbr);
        pnlChatArea.Controls.Add(new LiteralControl("</br>"));
        pnlChatArea.Controls.Add(lblnewline);
    
        
    
      }

    I hope this will help you.

    Thank you.

    Thursday, June 27, 2019 6:40 AM
  • User-418973555 posted

    nope,

    it only adds another </br> above the text 'new line',

    Thursday, June 27, 2019 6:46 AM
  • User-1038772411 posted

    hello larnvok09,

    kindly refer the following link, i hope that will help you

    https://forums.asp.net/t/2056673.aspx?Is+it+possible+to+insert+a+new+line+programmatically+

    If you still don't get your expected output, kindly share snapshot of your output.

    Thank you. 

    Thursday, June 27, 2019 7:27 AM
  • User-418973555 posted

    i think that example will only show one line,

    the Button_Click1 is for inserting text 'new line' to the panel everytime it clicked,

    eg. if clicked 9 times
    
    output : 
    
    newline
    newline
    newline
    newline
    newline
    newline
    newline
    newline
    newline
    
    
    
    current output:
    
    newline

    Thursday, June 27, 2019 7:35 AM
  • User665608656 posted

    Hi larnvok,

    According to your description, the reason why you still have only one 'new line' from the second click is that the button refreshes the page, causing the controls that were added to the panel to be refreshed.

    Therefore, I suggest that you store the current panel in the session after each panel content addition, and determine whether there is content in the session before adding controls to the panel.

    If there is, loop the controls properties of the panel in the session.

    Put all the original controls in the panel to store in a list first, and then loop this list and add the control to the current panel.

    Finally, the subsequent add action is executed.

    For more details, you could refer to the following code:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Session["cCounter"] = 0;
                    Session["panelcontent"] = null;
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["cCounter"] = Convert.ToInt64(Session["cCounter"]) + 1;
                Literal ltbr = new Literal();
                ltbr.ID = "ltbr" + Session["cCounter"].ToString();
                ltbr.Text = "</br>";
    
                Label lblnewline = new Label();
                lblnewline.ID = "lblnewline" + Session["cCounter"].ToString();
                lblnewline.Text = "newline";
    
                if (Session["panelcontent"] != null)
                {
                    Panel panel1 = Session["panelcontent"] as Panel;
                    List<Control> controls = new List<Control>();
                    foreach (Control item in panel1.Controls)
                    {
                        controls.Add(item);
                    }
                    for (int i = 0; i < controls.Count; i++)
                    {
                        pnlChatArea.Controls.Add(controls[i]);
                    }
                }
                pnlChatArea.Controls.Add(ltbr);
                pnlChatArea.Controls.Add(lblnewline);
    
                Session["panelcontent"] = pnlChatArea;
            }

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2019 8:17 AM