1

I want to add handling for dynamically created buttons. The closest code I can see on Stack is: asp.net dynamically button with event handler

I have code, below, that creates a button but I want to have a handler for each of these.

My code so far:

Dim control As LiteralControl
        control = New LiteralControl("<div class=""bar-row"">" & _
                                     "  <input value=""" & button & """ id=""" & id & """ class=""btnRisk"" type=""submit"">" & _
                                     "  <div class=""bar-data"">" & _
                                     "      <div class=""bar-inner"">" & _
                                     "          <div class=""bar bar-high"" style=""width: " & highPer & "%"">" & high & "</div>" & _
                                     "          <div class=""bar bar-med"" style=""width: " & medPer & "%"">" & medium & "</div>" & _                                      
                                     "      </div>" & _
                                     "  </div>" & _
                                     "</div>")
        ChartArea.Controls.Add(control)

The important part being:

"  <input value=""" & button & """ id=""" & id & """ class=""btnRisk""     type=""submit"">"

I'm not sure how to amend this.

I need to pass the button ID to a sender and then do something i.e.

Public Sub Buttons(ByVal sender As Object, ByVal e As EventArgs)

If sender = "1" Then
'do something
End If
If sender ="2" Then
'something else
End If 
End Sub

VERSION 2

ChartArea.Controls.Add(New LiteralControl("<div class=""bar-row"">"))

        Dim vRiskActive As String = "btnRisk "
        If id <> "btnMech" Then vRiskActive = vRiskActive + " risk-inactive"

        Dim btnButton As New Button() With {.Text = button, .ID = id, .CssClass = vRiskActive}
        AddHandler btnButton.Click, AddressOf button ' here Buttons is your Handler  

        ChartArea.Controls.Add(btnButton)

        If id = "ContentMain_btnMech" Then
            ChartArea.Controls.Add(New LiteralControl("  <div class=""bar-data"">" &
                                                     "      <div class=""bar-inner"">" &
                                                     "          <div class=""bar bar-high"" style=""width: " & highPer & "%"">" & high & "</div>" &
                                                     "          <div class=""bar bar-med"" style=""width: " & medPer & "%"">" & medium & "</div>" &
                                                      "          <div class=""bar bar-low"" style=""width: " & lowhPer & "%"">" & low & "</div>" &
                                                    "          <div class=""bar bar-na"" style=""width: " & naPer & "%"">" & na & "</div>" &
        "      </div>" &
                                                     "  </div>" &
                                                     "</div>"))
        Else
            ChartArea.Controls.Add(New LiteralControl("  <div class=""bar-data"">" &
                                     "      <div class=""bar-inner bar-inactive"">" &
                                    "  </div>" &
                                    "  </div>" &
                                                     "</div>"))
        End If

Worth noting Sub part...

Public Sub AddChartRow(ByVal button As String, ByVal id As String, ByVal high As Integer, ByVal medium As Integer, ByVal low As Integer, ByVal na As Integer)
Community
  • 1
  • 1
indofraiser
  • 1,014
  • 3
  • 18
  • 50
  • See [this answer](http://stackoverflow.com/a/389344/3740093) to how you can access elements of a `
    ` tag. If this is not a form tag then you should be using something like `` or just `
    – Visual Vincent Apr 26 '16 at 15:15
  • Hi @VisualVincent thanks for that. Issue I have is button is the submit. I've added Dim n As String n = [String].Format("{0}", Request.Form("ContentMain_btnMech")) to my pageload but don't pick anything up. :-( – indofraiser Apr 29 '16 at 09:34
  • So far I've got it to create: but it does not seem to click (I've added the .aspx.vb oncommand code too – indofraiser Apr 29 '16 at 10:10
  • Is there any special reason you are trying to use dynamic buttons/controls? Using these is to some extent working against the web forms programming model. You will have a *much* more pleasant time if you work *with* the web forms model... When user are asking about how to use dynamic controls it's almost always the case that either 1) They don't know how to do the same thing without dynamic controls, or 2) They think dynamic controls are cooler/more hardcore... – user1429080 Apr 29 '16 at 13:18
  • Because the amount of buttons to show varies by the client login and permissions and we can generate only required data then. Yes static is easier but on this occasion sadly not. – indofraiser Apr 29 '16 at 13:22
  • Can't you just use a `Repeater` then and bind it with data suitable for the current user? Anyway, I will not pester you more with this. If you truly think you must use dynamic controls, then by all means do that. My experience is still that it is almost never necessary to use them... – user1429080 Apr 29 '16 at 13:28
  • @indofraiser : I believe the correct handler is `onclick` rather than `oncommand`. – Visual Vincent Apr 29 '16 at 13:35

1 Answers1

1

I will suggest to change your HTML Code and create a Button. Also after creating a button you can try to AddHandler for an Event of your button.

ChartArea.Controls.Add(New LiteralControl("<div class=""bar-row"">"))

Dim btnButton As new Button() With { .Text = button,
                                     .Id = id,
                                     .CssClass = "btnRisk"
                                   }

AddHandler btnButton.Click, AddressOf ButtonsID ' here Buttons is your Handler        

ChartArea.Controls.Add(btnButton)
ChartArea.Controls.Add(New LiteralControl("  <div class=""bar-data"">" & _
                                             "      <div class=""bar-inner"">" & _
                                             "          <div class=""bar bar-high"" style=""width: " & highPer & "%"">" & high & "</div>" & _
                                             "          <div class=""bar bar-med"" style=""width: " & medPer & "%"">" & medium & "</div>" & _                                      
                                             "      </div>" & _
                                             "  </div>" & _
                                             "</div>"))
                ChartArea.Controls.Add(control)

More info about AdressOf Operator can be find in MSDN

indofraiser
  • 1,014
  • 3
  • 18
  • 50
Mitrucho
  • 126
  • 9