indofraiser indofraiser - 7 months ago 33
Vb.net Question

Create handler for dynamic buttons

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)

Answer

I will suggest to change your HTML Code and create a Button. Also after creating a button you can try to AddHanlder 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, AdressOf Buttons ' here Buttons is your Hanlder        

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

Comments