locked
Adding Button Click Event for button to panel inside update panel RRS feed

  • Question

  • User-418973555 posted

    trying to add button event but does not work

     Button ButtonFinish = new Button()
          {
            ID = "btnFinish",
            Text = "Finish"
          };
    
          ButtonFinish.Attributes.Add("runat", "server");
          ButtonFinish.Click += new EventHandler(this.ButtonFinish_Click);
    
    
          pnlChatArea.Controls.Add(ButtonFinish);
          udpChatArea.Update();
    
    
      private void ButtonFinish_Click(object sender, EventArgs e)
      {
        lblErrMsg.Text = "sadsddasda";
        udpfreetextbox.Update();
    
    
      }
    
     <asp:UpdatePanel ID="udpfreetextbox" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
              <p><asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" /></p>
           
              </ContentTemplate>
          <Triggers>
              <asp:AsyncPostBackTrigger ControlID="Button2" />
          </Triggers>
          </asp:UpdatePanel>

    Monday, July 22, 2019 4:43 AM

Answers

  • User665608656 posted

    Hi larnvok,

    According to your requirements,I modified your code appropriately to run on my machine.

    I suggest you should use variable i rather than Session["sChatCounterplus1"] to generate unique ID while you read your data and create controls.

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

            protected void Page_Init(object sender, EventArgs e)
            {
                if (IsPostBack)
                {
                    if (Session["button"] != null)
                    {
                        if (Session["button"].ToString().Contains("btnFinish"))
                        {
                            CreateFun();
                        }
                    }
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Session["button"] = null;
                }
            }
    
            private void ButtonFinish_Click(object sender, CommandEventArgs e)
            {
                if ((String)e.CommandArgument != "")
                {
                    lblErrMsg.Text = e.CommandArgument.ToString();
                    udpfreetextbox.Update();
                }
    
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                if (Session["button"] == null)
                {
                    Session["button"] = "btnFinish";
                    CreateFun();
                }
            }
            public void CreateFun()
            {
                using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    using (SqlCommand cmd2 = new SqlCommand("SELECT * from employee"))
                    {
                        cmd2.Connection = con2;
                        con2.Open();
                        using (SqlDataReader rdr2 = cmd2.ExecuteReader())
                        {
                            if (rdr2.HasRows)
                            {
                                int i = 0;
                                while (rdr2.Read())
                                {
                                    //if (Convert.ToInt32(rdr2["acsadminislogin"]) == 0)
                                    //{
                                    Literal ltbr3 = new Literal();
                                    // Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr3.ID = "ltbr3" + i.ToString();
                                    ltbr3.Text = "<div style=\"padding-left:2px; ; height:80px; bottom:0px; left:60px; width: 300px; text-wrap:normal; background-color:aliceblue; \">";
                                    pnlChatArea.Controls.Add(ltbr3);
                                    udpChatArea.Update();
    
    
                                    Literal ltbr2 = new Literal();
                                    //Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr2.ID = "ltbr2" + i.ToString();
                                    ltbr2.Text = "</br>";
                                    pnlChatArea.Controls.Add(ltbr2);
                                    udpChatArea.Update();
    
                                    Literal ltbr31 = new Literal();
                                    // Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr31.ID = "ltbr4" + i.ToString();
                                    ltbr31.Text = "<div style=\"padding-left:2px; height:10px; ; top:0px; right:60px; width: 50px; \">";
                                    pnlChatArea.Controls.Add(ltbr31);
                                    udpChatArea.Update();
    
                                    Label lblguest = new Label();
                                    lblguest.ID = "lblguest" + i.ToString();
                                    lblguest.Text = "Guest: ";
                                    pnlChatArea.Controls.Add(lblguest);
    
                                    Literal ltbr41 = new Literal();
                                    //  Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr41.ID = "ltbr5" + i.ToString();
                                    ltbr41.Text = "</div>";
                                    pnlChatArea.Controls.Add(ltbr41);
                                    udpChatArea.Update();
    
                                    //}
    
                                    //Label lblguest2 = new Label();
                                    //lblguest2.ID = "lblguest2" + Session["sChatCounterplus1"].ToString();
                                    //lblguest2.Text = "Guest: ";
                                    //pnlChatArea.Controls.Add(lblguest2);
    
    
                                    Label lblnewline = new Label();
                                    lblnewline.ID = "lblnewline" + i.ToString();
    
                                    System.Web.UI.WebControls.Image NewImage = new System.Web.UI.WebControls.Image();
                                    NewImage.ID = "imgemoji" + i.ToString();
                                    NewImage.Height = Unit.Pixel(25);
                                    NewImage.Width = Unit.Pixel(25);
                                    //};
    
                                    //if (rdr2["acsclientdatetime"].ToString() != "")
                                    //{
    
                                    lblnewline.Text = rdr2["FirstName"].ToString();
                                    pnlChatArea.Controls.Add(lblnewline);
                                    udpChatArea.Update();
    
                                    //}
    
                                    Button btnConnect = new Button()
                                    {
                                        ID = "btnConnect" + i.ToString(),
                                        Text = "Connect"
                                    };
                                    btnConnect.CommandArgument = rdr2["LastName"].ToString();
                                    btnConnect.Command += ButtonFinish_Click;
                                    // ButtonFinish.Click += new EventHandler(ButtonFinish_Click);
                                    pnlChatArea.Controls.Add(btnConnect);
                                    udpChatArea.Update();
    
                                    Literal ltbr4 = new Literal();
                                    //Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr4.ID = "ltbr6" + i.ToString();
                                    ltbr4.Text = "</div>";
                                    pnlChatArea.Controls.Add(ltbr4);
                                    udpChatArea.Update();
    
                                    Literal ltbr = new Literal();
                                    ltbr.ID = "ltbr7" + i.ToString();
                                    ltbr.Text = "</br>";
                                    pnlChatArea.Controls.Add(ltbr);
                                    udpChatArea.Update();
    
                                    i++;
                                }
    
                            }
                        }
                        //ScriptManager.RegisterStartupScript(pnlChatArea, pnlChatArea.GetType(), "asetscrollup", "setscrollup();", true);
                    }
    
                }
            }

    The result of this work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 25, 2019 6:47 AM

All replies

  • User665608656 posted

    Hi larnvok,

    According to your description, I tested it with the code you provided. Because the code you provided is not comprehensive enough, I am not sure if you have any special requirements.

    I put the code you created btnFinish in the page_load event, and everything works well.

    I hope you can provide more detailed code and comprehensive requirements so that I can solve your issue more easily.

    Here is the demo based on your code , you could refer to it:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="udpfreetextbox" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <p>
                        <asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" />
                    </p>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button2" />
                </Triggers>
            </asp:UpdatePanel>
             <asp:UpdatePanel ID="udpChatArea" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Panel ID="pnlChatArea" runat="server"></asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:Button ID="Button2" runat="server" Text="Button2"  />
        </form>
    </body>
    </html>
    
           protected void Page_Load(object sender, EventArgs e)
            {
                // Make sure this code is in your page_load event.  Also give the button an ID
                Button ButtonFinish = new Button()
                {
                    ID = "btnFinish",
                    Text = "Finish"
                };
    
                ButtonFinish.Attributes.Add("runat", "server");
                ButtonFinish.Click += new EventHandler(this.ButtonFinish_Click);
                pnlChatArea.Controls.Add(ButtonFinish);
                udpChatArea.Update();
            }
    
            private void ButtonFinish_Click(object sender, EventArgs e)
            {
                lblErrMsg.Text = "sadsddasda";
                udpfreetextbox.Update();
            }

    The result of this work demo:

    Best Regards,

    YongQing.

    Monday, July 22, 2019 9:36 AM
  • User-418973555 posted
    okay, but if it possible to place it somewhere around eg. if not postback ,
    Monday, July 22, 2019 10:04 AM
  • User665608656 posted

    Hi larnvok,

    okay, but if it possible to place it somewhere around eg. if not postback ,

    Yes, I guess you want to create the finish button in Button2 click event, right?

    However, according to ASP.NET page lifecycle, in order for new button to fire it's click event on postback, after you create new button's event in other methods, they need to be recreated and attached to the event handler again in the page_init event. 

    You can refer to this link:  https://stackoverflow.com/questions/8779040/aspbutton-click-event-not-being-fired

    I have created new button and it's click event in the Button2 event based on your code, you could refer to the following code, hope it will help you.

    The front code is the same as the previous reply.

            protected void Page_Init(object sender, EventArgs e)
            {
    //create button again if (Session["button"] != null) { if (Session["button"].ToString().Contains("btnFinish")) { Button ButtonFinish = new Button() { ID = "btnFinish", Text = "Finish" }; ButtonFinish.Click += new EventHandler(ButtonFinish_Click); pnlChatArea.Controls.Add(ButtonFinish); udpChatArea.Update(); } } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) {
    // when you refresh you page,the new button should be disappear Session["button"] = null; foreach (Control item in pnlChatArea.Controls.OfType<Button>()) { if (item.ID == "btnFinish") pnlChatArea.Controls.Remove(item); } } } private void ButtonFinish_Click(object sender, EventArgs e) { lblErrMsg.Text = "sadsddasda"; udpfreetextbox.Update(); } protected void Button2_Click(object sender, EventArgs e) {
    // create new button in Button2 click event. if (Session["button"] == null) { Session["button"] = "btnFinish"; Button ButtonFinish = new Button() { ID = "btnFinish", Text = "Finish" }; ButtonFinish.Click += new EventHandler(ButtonFinish_Click); pnlChatArea.Controls.Add(ButtonFinish); udpChatArea.Update(); } }

    The result of this work demo:

    Best Regards,

    YongQing.

    Tuesday, July 23, 2019 1:52 AM
  • User-418973555 posted

    okay, however by creating dynamic button, tried this

        Button btnConnect = new Button();
                   btnConnect.ID = "btnConnect" + Session["sChatCounterplus1"].ToString();
                  //btnConnect.ID = "btnConnect";
                  btnConnect.Text = "Connect";
                  btnConnect.Attributes.Add("runat", "server");
                  btnConnect.CommandArgument = rdr2["acsid"].ToString();
                  btnConnect.Attributes.Add("OnCommand", "ButtonFinish_Click");
    
    
    
      private void ButtonFinish_Click(object sender, CommandEventArgs e)
      {
        if ((String)e.CommandArgument != "")
        {
    
          lblErrMsg.Text = e.CommandArgument.ToString();
          udpfreetextbox.Update();
        }

    the e.CommandArgument seems to show no value in lblErrMsg.Text,

    Wednesday, July 24, 2019 6:51 AM
  • User665608656 posted

    Hi larnvok,

    According to your code, OnCommand is a method of button, not an attribute, so you need to create the OnCommand method as you create the OnClick method.

    In addition, you need to use CommandEventHandler when creating the OnCommand method, which is different from the OnClick method.

    Then, ButtonFinish_Click here is OnCommand event now, you need to change the second parameter of ButtonFinish_Click that's to say change EventArgs to CommandEventArgs to ensure that the types are consistent.

    You could refer to this link: Button.OnCommand(CommandEventArgs) Method 

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

     protected void Page_Init(object sender, EventArgs e)
            {
                if (Session["button"] != null)
                {
                    if (Session["button"].ToString().Contains("btnFinish"))
                    {
                        Button ButtonFinish = new Button()
                        {
                            ID = "btnFinish",
                            Text = "Finish"
                        };
                        ButtonFinish.CommandArgument = "123";
                        ButtonFinish.Command += new CommandEventHandler(ButtonFinish_Click);
                        // ButtonFinish.Click += new EventHandler(ButtonFinish_Click);
                        pnlChatArea.Controls.Add(ButtonFinish);
                        udpChatArea.Update();
                    }
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Session["button"] = null;
                    foreach (Control item in pnlChatArea.Controls.OfType<Button>())
                    {
                        if (item.ID == "btnFinish")
                            pnlChatArea.Controls.Remove(item);
                    }
                }
            }
    
            private void ButtonFinish_Click(object sender, CommandEventArgs e)
            { 
                if ((String)e.CommandArgument != "")
                {
                    lblErrMsg.Text = e.CommandArgument.ToString();
                    udpfreetextbox.Update();
                }
    
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                if (Session["button"] == null)
                {
                    Session["button"] = "btnFinish";
                    Button ButtonFinish = new Button()
                    {
                        ID = "btnFinish",
                        Text = "Finish"
                    };
                    ButtonFinish.CommandArgument = "123";
                    ButtonFinish.Command += new CommandEventHandler(ButtonFinish_Click);
                    // ButtonFinish.Click += new EventHandler(ButtonFinish_Click);
                    pnlChatArea.Controls.Add(ButtonFinish);
                    udpChatArea.Update();
                }
    
            }

    The result of this work demo:

    Best Regards,

    YongQing.

    Wednesday, July 24, 2019 7:58 AM
  • User-418973555 posted

    yea, i place the below code just to create button with fixed ID, it works fine in the preinit; but when place it all the code in preinit, it will not work despite the grid with button shows

    ...
    Button btnConnect = new Button()
                  {
                  
    
                    ID = "btnConnect1"
                  
                    Text = "Connect"
                  };
                 
                  btnConnect.CommandArgument = "123"; //rdr2["acsid"].ToString();
                 
                  btnConnect.Command += new CommandEventHandler(ButtonFinish_Click);
               
    
                  pnlChatArea.Controls.Add(btnConnect);
                  udpChatArea.Update();
    ...
    
     #region Display message by msgid
    
        using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ABCDConnectionString"].ConnectionString))
        {
          Regex r1 = new Regex("(:>)");
          Regex r2 = new Regex("(:~)");
          Regex r3 = new Regex("(:/)");
          Regex r4 = new Regex("(:>>)");
          Regex renoncharspec = new Regex("[a-zA-Z._^%$#!~@,-]+");
    
    
          using (SqlCommand cmd2 = new SqlCommand("SELECT [AdminChatSession].id as acsid, [AdminChatSession].adminislogin AS acsadminislogin, [AdminChatSession].clientdatetime AS acsclientdatetime " +
              " FROM [AdminChatSession] " +
              " WHERE [AdminChatSession].adminislogin = 0"))
          {
            cmd2.Connection = con2;
            con2.Open();
    
            using (SqlDataReader rdr2 = cmd2.ExecuteReader())
            {
              if (rdr2.HasRows)
              {
    
                while (rdr2.Read())
                {
    
                  if (Convert.ToInt32(rdr2["acsadminislogin"]) == 0)
                  {
                    Literal ltbr3 = new Literal();
                    Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                    ltbr3.ID = "ltbr3" + Session["sChatCounterplus1"].ToString();
                    ltbr3.Text = "<div style=\"padding-left:2px; ; height:80px; bottom:0px; left:60px; width: 300px; text-wrap:normal; background-color:aliceblue; \">";
                    pnlChatArea.Controls.Add(ltbr3);
                    udpChatArea.Update();
    
    
                    Literal ltbr2 = new Literal();
                    Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                    ltbr2.ID = "ltbr2" + Session["sChatCounterplus1"].ToString();
                    ltbr2.Text = "</br>";
                    pnlChatArea.Controls.Add(ltbr2);
                    udpChatArea.Update();
    
                    Literal ltbr31 = new Literal();
                    Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                    ltbr31.ID = "ltbr31" + Session["sChatCounterplus1"].ToString();
                    ltbr31.Text = "<div style=\"padding-left:2px; height:10px; ; top:0px; right:60px; width: 50px; \">";
                    pnlChatArea.Controls.Add(ltbr31);
                    udpChatArea.Update();
    
                    Label lblguest = new Label();
                    lblguest.ID = "lblguest" + Session["sChatCounterplus1"].ToString();
                    lblguest.Text = "Guest: ";
                    pnlChatArea.Controls.Add(lblguest);
    
                    Literal ltbr41 = new Literal();
                    Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                    ltbr41.ID = "ltbr41" + Session["sChatCounterplus1"].ToString();
                    ltbr41.Text = "</div>";
                    pnlChatArea.Controls.Add(ltbr41);
                    udpChatArea.Update();
    
                  }
    
                  //Label lblguest2 = new Label();
                  //lblguest2.ID = "lblguest2" + Session["sChatCounterplus1"].ToString();
                  //lblguest2.Text = "Guest: ";
                  //pnlChatArea.Controls.Add(lblguest2);
    
    
                  Label lblnewline = new Label();
                  lblnewline.ID = "lblnewline" + Session["sChatCounterplus1"].ToString();
    
                  System.Web.UI.WebControls.Image NewImage = new System.Web.UI.WebControls.Image();
                  NewImage.ID = "imgemoji" + Session["sChatCounterplus1"].ToString();
                  NewImage.Height = Unit.Pixel(25);
                  NewImage.Width = Unit.Pixel(25);
    
    
    
    
    
    
                  //};
    
                  if (rdr2["acsclientdatetime"].ToString() != "")
                  {
    
                    lblnewline.Text = rdr2["acsclientdatetime"].ToString();
                    pnlChatArea.Controls.Add(lblnewline);
                    udpChatArea.Update();
    
                  }
                  //Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                  Button btnConnect = new Button()
                  {
                    //  btnConnect.ID = "btnConnect" + Session["sChatCounterplus1"].ToString();
                    ////btnConnect.ID = "btnConnect";
                    //  btnConnect.Text = "Connect";
    
                    ID = "btnConnect" + Session["sChatCounterplus1"].ToString(),
                    //ID = "btnConnect1",
                    Text = "Connect"
                  };
                  // btnConnect.Attributes.Add("runat", "server");
                  btnConnect.CommandArgument = "123"; //rdr2["acsid"].ToString();
                  //btnConnect.Attributes.Add("OnCommand", "ButtonFinish_Click");
                  btnConnect.Command += new CommandEventHandler(ButtonFinish_Click);
                  // btnConnect.OnCommand = 
                  // btnConnect.Click += new EventHandler(this.ButtonFinish_Click);
    
                  pnlChatArea.Controls.Add(btnConnect);
                  udpChatArea.Update();
    
                  //pnlChatArea.Controls.Add(btnConnect);
                  //udpChatArea.Update();
    
                  Literal ltbr4 = new Literal();
                  Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                  ltbr4.ID = "ltbr4" + Session["sChatCounterplus1"].ToString();
                  ltbr4.Text = "</div>";
                  pnlChatArea.Controls.Add(ltbr4);
                  udpChatArea.Update();
    
                  Literal ltbr = new Literal();
                  ltbr.ID = "ltbr" + Session["sChatCounterplus1"].ToString();
                  ltbr.Text = "</br>";
                  pnlChatArea.Controls.Add(ltbr);
                  udpChatArea.Update();
    
                  Session["sMsgPartIDRead"] = null;
                }
    
              }
            }
            ScriptManager.RegisterStartupScript(pnlChatArea, pnlChatArea.GetType(), "asetscrollup", "setscrollup();", true);
          }
        }
      private void ButtonFinish_Click(object sender, CommandEventArgs e)
      {
        //if (Session["sButton"] == null)
        //{
    
        //  Session["sButton"] = "btn";
          if ((String)e.CommandArgument != "")
          {
    
            lblErrMsg.Text = e.CommandArgument.ToString();
            udpfreetextbox.Update();
          }

    Wednesday, July 24, 2019 8:34 AM
  • User665608656 posted

    Hi larnvok,

    According to your requirements,I modified your code appropriately to run on my machine.

    I suggest you should use variable i rather than Session["sChatCounterplus1"] to generate unique ID while you read your data and create controls.

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

            protected void Page_Init(object sender, EventArgs e)
            {
                if (IsPostBack)
                {
                    if (Session["button"] != null)
                    {
                        if (Session["button"].ToString().Contains("btnFinish"))
                        {
                            CreateFun();
                        }
                    }
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Session["button"] = null;
                }
            }
    
            private void ButtonFinish_Click(object sender, CommandEventArgs e)
            {
                if ((String)e.CommandArgument != "")
                {
                    lblErrMsg.Text = e.CommandArgument.ToString();
                    udpfreetextbox.Update();
                }
    
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                if (Session["button"] == null)
                {
                    Session["button"] = "btnFinish";
                    CreateFun();
                }
            }
            public void CreateFun()
            {
                using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    using (SqlCommand cmd2 = new SqlCommand("SELECT * from employee"))
                    {
                        cmd2.Connection = con2;
                        con2.Open();
                        using (SqlDataReader rdr2 = cmd2.ExecuteReader())
                        {
                            if (rdr2.HasRows)
                            {
                                int i = 0;
                                while (rdr2.Read())
                                {
                                    //if (Convert.ToInt32(rdr2["acsadminislogin"]) == 0)
                                    //{
                                    Literal ltbr3 = new Literal();
                                    // Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr3.ID = "ltbr3" + i.ToString();
                                    ltbr3.Text = "<div style=\"padding-left:2px; ; height:80px; bottom:0px; left:60px; width: 300px; text-wrap:normal; background-color:aliceblue; \">";
                                    pnlChatArea.Controls.Add(ltbr3);
                                    udpChatArea.Update();
    
    
                                    Literal ltbr2 = new Literal();
                                    //Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr2.ID = "ltbr2" + i.ToString();
                                    ltbr2.Text = "</br>";
                                    pnlChatArea.Controls.Add(ltbr2);
                                    udpChatArea.Update();
    
                                    Literal ltbr31 = new Literal();
                                    // Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr31.ID = "ltbr4" + i.ToString();
                                    ltbr31.Text = "<div style=\"padding-left:2px; height:10px; ; top:0px; right:60px; width: 50px; \">";
                                    pnlChatArea.Controls.Add(ltbr31);
                                    udpChatArea.Update();
    
                                    Label lblguest = new Label();
                                    lblguest.ID = "lblguest" + i.ToString();
                                    lblguest.Text = "Guest: ";
                                    pnlChatArea.Controls.Add(lblguest);
    
                                    Literal ltbr41 = new Literal();
                                    //  Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr41.ID = "ltbr5" + i.ToString();
                                    ltbr41.Text = "</div>";
                                    pnlChatArea.Controls.Add(ltbr41);
                                    udpChatArea.Update();
    
                                    //}
    
                                    //Label lblguest2 = new Label();
                                    //lblguest2.ID = "lblguest2" + Session["sChatCounterplus1"].ToString();
                                    //lblguest2.Text = "Guest: ";
                                    //pnlChatArea.Controls.Add(lblguest2);
    
    
                                    Label lblnewline = new Label();
                                    lblnewline.ID = "lblnewline" + i.ToString();
    
                                    System.Web.UI.WebControls.Image NewImage = new System.Web.UI.WebControls.Image();
                                    NewImage.ID = "imgemoji" + i.ToString();
                                    NewImage.Height = Unit.Pixel(25);
                                    NewImage.Width = Unit.Pixel(25);
                                    //};
    
                                    //if (rdr2["acsclientdatetime"].ToString() != "")
                                    //{
    
                                    lblnewline.Text = rdr2["FirstName"].ToString();
                                    pnlChatArea.Controls.Add(lblnewline);
                                    udpChatArea.Update();
    
                                    //}
    
                                    Button btnConnect = new Button()
                                    {
                                        ID = "btnConnect" + i.ToString(),
                                        Text = "Connect"
                                    };
                                    btnConnect.CommandArgument = rdr2["LastName"].ToString();
                                    btnConnect.Command += ButtonFinish_Click;
                                    // ButtonFinish.Click += new EventHandler(ButtonFinish_Click);
                                    pnlChatArea.Controls.Add(btnConnect);
                                    udpChatArea.Update();
    
                                    Literal ltbr4 = new Literal();
                                    //Session["sChatCounterplus1"] = Convert.ToInt32(Session["sChatCounterplus1"]) + 1;
                                    ltbr4.ID = "ltbr6" + i.ToString();
                                    ltbr4.Text = "</div>";
                                    pnlChatArea.Controls.Add(ltbr4);
                                    udpChatArea.Update();
    
                                    Literal ltbr = new Literal();
                                    ltbr.ID = "ltbr7" + i.ToString();
                                    ltbr.Text = "</br>";
                                    pnlChatArea.Controls.Add(ltbr);
                                    udpChatArea.Update();
    
                                    i++;
                                }
    
                            }
                        }
                        //ScriptManager.RegisterStartupScript(pnlChatArea, pnlChatArea.GetType(), "asetscrollup", "setscrollup();", true);
                    }
    
                }
            }

    The result of this work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 25, 2019 6:47 AM
  • User-418973555 posted

    okay, that works --->

    btnConnect.Command += ButtonFinish_Click;

    and think dunno why having re-plus the Session["sChatCounterplus1"] at some part,

    Thursday, July 25, 2019 7:14 AM
  • User-1279664359 posted

    okay, that works --->

    btnConnect.Command += ButtonFinish_Click;

    and think dunno why having re-plus the Session["sChatCounterplus1"] at some part,

    Hope this works

    Friday, July 26, 2019 6:08 AM