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)
7
  • See this answer to how you can access elements of a <form> tag. If this is not a form tag then you should be using something like <asp:Button> or just <button> instead of <input>. Commented Apr 26, 2016 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. :-( Commented Apr 29, 2016 at 9:34
  • So far I've got it to create: <asp:button runat="server" commandargument="ContentMain_btnMech" oncommand="btnRiskItem" id="ContentMain_btnMech" text="MECHANICAL"></asp:button> but it does not seem to click (I've added the .aspx.vb oncommand code too Commented Apr 29, 2016 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... Commented Apr 29, 2016 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. Commented Apr 29, 2016 at 13:22

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

It says "value is not a member of the button"
sorry, obviously there is no such property in VB Button, please try .Text property. I edited an answer.
Thanks, just making sure. Sometimes it's the question asker :-)
AddHandler btnButton.Click, AddressOf button.ToString I believe?
no, you confused your button variable that is a string and Buttons that is Handler of your's VB Button. AddressOf operator requires a Delegate rather than string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.