Button dynamicaly created click event not working

29 views Asked by At

I would like to create a lot of buttons with different text and background and when I Click on these buttons, i would like to transfer Text and Colors of this button as Parameters for another Function.

But Dynamicaly Created Button call PostBack when clicked and not execute his on click events

Here is my codebehind used to create buttons :

for (int i = 0; i < elem; i++)

            {
                decimal vol = Convert.ToDecimal(DT.Rows[Liste[j]].ItemArray[3].ToString());

               

                TableRow rowct = new TableRow();
                TableCell CT1 = new TableCell();


                var button = new Button
                {
                    ID = "Button" + Moda + j,
                    CssClass = "TimatButton",

                    CommandArgument = DT.Rows[Liste[j]].ItemArray[6].ToString(),
                    Text = DT.Rows[Liste[j]].ItemArray[1].ToString(),
                    BackColor = System.Drawing.ColorTranslator.FromHtml(DT.Rows[Liste[j]].ItemArray[6].ToString()),
                    ForeColor = System.Drawing.ColorTranslator.FromHtml(DT.Rows[Liste[j]].ItemArray[8].ToString()),
                    BorderColor = System.Drawing.ColorTranslator.FromHtml(DT.Rows[Liste[j]].ItemArray[6].ToString()),
                    CommandName = "Load_Items",
                                       


            };
                
                if (vol != 1)
                { button.CssClass = "TimatCutButton"; }

               

                CT1.Controls.Add(button);

                AsyncPostBackTrigger Trig = new AsyncPostBackTrigger();
                Trig.ControlID = button.ID;
                Trig.EventName = "Click";
                UpdatePanel2.Triggers.Add(Trig);

                j += 1;
  protected void Load_Items(object sender, EventArgs e)
        {

            Button a = sender as Button;
            System.Diagnostics.Debug.WriteLine(a.Text);
            
        }

Actually void Load_Items is just to test code

and the front

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
                          </asp:ScriptManager>

                 <div style="width:100%;height:20%;display:flex;flex-direction:row">

                      <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" >
                           <ContentTemplate>
                     
                                <asp:HiddenField runat="server" ID="Colortxt"  />
                                <asp:HiddenField ID="Txtt" runat="server"/>

                      <table>
                        <tr >

                            <td class="BoxModality">
                              
                                <h4 style="text-align:center;color:darkblue">CT</h4>

                               <asp:table ID="tableCT" runat="server" style="margin:auto"></asp:table>                                              
                            </td>

                            <td class="BoxModality">
                                <h4 style="text-align:center;color:darkblue">RX</h4>
                                <asp:table ID="tablerx" runat="server" style="margin:auto"></asp:table>
                            </td>

                            <td class="BoxModality">
                                
                                <h4 style="text-align:center;color:darkblue">IRM</h4>
                      <asp:table ID="tableIRM" runat="server" style="margin:auto"></asp:table>
                            </td>

                          <td class="BoxModality">
                    
                         <h4 style="text-align:center;color:darkblue">SENO</h4>
                              <asp:table ID="tableSeno" runat="server" style="margin:auto"></asp:table>
                   
                         </td>

                               <td class="BoxModality">
                   
                         <h4 style="text-align:center;color:darkblue">MN</h4>
                                   <asp:table ID="tableMN" runat="server" style="margin:auto"></asp:table>
                   

                                   </td>
                            <td class="BoxModality">
                    
                         <h4 style="text-align:center;color:darkblue">ANGIO</h4>
                                <asp:table ID="tableXA" runat="server" style="margin:auto"></asp:table>
                   

                               </td>

                               <td class="BoxModality">
                   
                         <h4 style="text-align:center;color:darkblue">Secrétaires</h4>
                                   <asp:table ID="tableSec" runat="server" style="margin:auto">

                                    
                                   </asp:table>
                                     

                               </td>

                               <td class="BoxModality">
                  
                         <h4 style="text-align:center;color:darkblue">No Prest</h4>
                                   <asp:table ID="tableNOP" runat="server" style="margin:auto"></asp:table>
                  
                                   </td>
                  
                            <td style="margin:auto" >
                   
                         <asp:Button id="RunNewTimat" runat="server" OnClick="RunNewTimat_Click" Text="New Timat" CssClass="MiniButton"/>
                                </td>
                      </tr>
                          </table>

                    </ContentTemplate>
                          
                                </asp:UpdatePanel>
                    


                
                 </div>

Before this, I my code worked fine with FrontSide created buttons.

Great Thanks for yout help

0

There are 0 answers