我想爲動態創建的按鈕添加處理。最近的代碼,我可以看到堆棧是:asp.net dynamically button with event handler爲動態按鈕創建處理程序
我有代碼,下面,創建一個按鈕,但我想有一個處理程序爲每個這些。
我迄今爲止代碼:
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)
最重要的部分是:
" <input value=""" & button & """ id=""" & id & """ class=""btnRisk"" type=""submit"">"
我不知道如何修改這個。
我需要的按鈕ID傳遞給發送者,然後做一些即
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
值得注意的副部...
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)
參見[這個答案](http://stackoverflow.com/a/389344/3740093)被找到如何訪問的'
嗨@VisualVincent謝謝你。我有的問題是按鈕是提交。我已將Dim n As String n = [String] .Format(「{0}」,Request.Form(「ContentMain_btnMech」))添加到我的頁面加載中,但不要選擇任何內容。 :-( – indofraiser
到目前爲止我已經創建了它:但似乎沒有點擊(我已經添加了.aspx.vb oncommand代碼) –
indofraiser