Following the example answer found at ASP.NET/HTML: HTML button's onClick property inside ASP.NET (.cs)
The button "Click Me!"
works if I add the button html normally on the page. If I add it to the page using ASP.NET VB.NET InnerHTML
, the click doesn't fire. Does anyone know why?
Below is after the HTML is rendered.
Below is the ASP.NET VB.NET code:
strTbl1 = "<table style='text-align:Left;'><tr class='spaceUnder'>"
strTbl1 = strTbl1 + "<td style=''><button id='Button1' OnServerClick='Button1_OnClick' runat='server'>Click me!</button></td></tr>"
strTbl1 = strTbl1 + "</table>"
Task1Assignees.InnerHtml = strTbl1
I have attempted <input type=button...
, asp:Button ..
and etc but all don't register click event. If I attempt an asp type then I get a JavaScript error as well.
UPDATE:
Server Side function
Protected Sub Button1_OnClick(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("hi")
End Sub
FINAL FIX
So using example from Dynamic created Buttons VB.Net with Loop and from the accepted answer below, I was able to fix my code like so
Page_Load (outside the IsPostBack check)
Task1Assignees.Controls.Clear()
For j As Integer = 0 To System.Web.HttpContext.Current.Session("intAssignees") Step 1
Dim btn As New Button
btn.Text = "Mark Completed"
btn.CssClass = "btn btn-success"
btn.ID = "btnAssignee" & j
AddHandler btn.Click, AddressOf Send
Task1Assignees.Controls.Add(btn)
Next
Button Creation
System.Web.HttpContext.Current.Session("intAssignees") = ds1.Tables(0).Rows.Count
Task1Assignees.Controls.Clear()
For j As Integer = 0 To System.Web.HttpContext.Current.Session("intAssignees") Step 1
Dim btn As New Button
btn.Text = "Mark Completed"
btn.Cssclass = "btn btn-success"
btn.ID = "btnAssignee" & j
AddHandler btn.Click, AddressOf Send
Task1Assignees.Controls.Add(btn)
Next
Function for button
Sub Send()
Response.Redirect("~\Home.aspx")
End Sub