Paul Rajs

Paul Rajs

  • NA
  • 641
  • 147k

Dynamic button control Second Click is Not Working but third

Feb 28 2018 3:23 AM
hi developers ,
 
i am created a dynamic control in asp:Panel. its fine. i want to change the label number in each click of the dynamic created button. when if i clicking the dynamic control button the first click is working well , but the second click its not working , but the third click is working well.its working like a even method. so i need to click twice a button.
 
so how can i solve this problem . i have done all but this is the only and major issue in my module.
 
below am adding all of my codes . all of you go through and if anyone know the solution to my problem please suggest me to how can i done this task.
 
Html aspx Page Code
  1. <asp:Panel runat="server" ID="pnl_btns" CssClass="exm-btn"></asp:Panel>  
  2. <asp:Button ID="btnLoadButton" runat="server" Text="Button" Visible="false" OnClick="btnLoadButton_Click"></asp:Button>  
  3. <asp:Label ID="lbl" runat="server" ForeColor="Red" Font-Size="Larger"></asp:Label>  
C# Code Behind Page
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!IsPostBack)  
  4. {  
  5. bind();  
  6. }  
  7. if (IsPostBack)  
  8. {  
  9. bind();  
  10. }  
  11. }  
  12. protected void btnLoadButton_Click(object sender, EventArgs e)  
  13. {  
  14. pnl_btns.Controls.Clear();  
  15. string text = (sender as Button).Text;  
  16. if (text == "1")  
  17. {  
  18. lbl.Text = text;  
  19. lbl.ForeColor = Color.Green;  
  20. }  
  21. else if (text == "2")  
  22. {  
  23. lbl.Text = text;  
  24. lbl.ForeColor = Color.Red;  
  25. }  
  26. else  
  27. {  
  28. lbl.Text = text;  
  29. lbl.ForeColor = Color.Yellow;  
  30. }  
  31. bind();  
  32. }  
  33. public void bind()  
  34. {  
  35. for (int i = 0; i < 5; i++)  
  36. {  
  37. Button btn = new Button();  
  38. btn.Text = "" + (i + 1);  
  39. btn.Click += btnLoadButton_Click;  
  40. pnl_btns.Controls.Add(btn);  
  41. }  
  42. }  
thanking you
Paul.S

Answers (1)