0

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.

enter image description here

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
narue1992
  • 1,143
  • 1
  • 14
  • 40

2 Answers2

0

That won't work, but you can create server-side HTML controls instead.

var table = new HtmlTable();
var row = new HtmlTableRow();
var cell = new HtmlTableCell();
var btn = new LinkButton();

btn.Text = "Click Me!";
btn.Click += Button1_OnClick;

cell.Controls.Add(btn);
row.Cells.Add(cell);
table.Rows.Add(row);
Task1Assignees.Controls.Add(table);
hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • when looking in vb.net there is no `btn.Click` event. only `onclientclick`. That is why I assumed I couldn't use this. – narue1992 Mar 19 '18 at 14:40
  • Sorry, I missed the VB.NET part, you can do something like this I think: `AddHandler btn.Click, AddressOf Button1_OnClick` – hardkoded Mar 19 '18 at 14:42
  • I did the following `sB.ID = "Button1" sB.Text = "Mark Completed" cell.Controls.Add(sB) row.Cells.Add(cell) table.Rows.Add(row) Task1Assignees.Controls.Add(table)` with the `AddHandler sB.Click, AddressOf Button1_OnClick` in the `Page_load` and the click event didn't fire :/ . no MsgBox showed – narue1992 Mar 19 '18 at 14:48
  • Are you using the native MsgBox? That's not going to work on the web. – hardkoded Mar 19 '18 at 14:51
  • I am just putting that `MsgBox` command there with a debug point. The event doesn't fire because the debug point doesn't get reached. – narue1992 Mar 19 '18 at 14:56
  • I just created a page (in C#) and it worked https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/Demos/ControlCreation.aspx.cs Remember you need to recreate controls in every postback – hardkoded Mar 19 '18 at 15:03
  • Issue is I have to create 'x' amount of this button depending on a counter I determine from another control click on the page. So not sure if this will work then because I will do the Controls.Add 'x' amount of times – narue1992 Mar 19 '18 at 15:06
  • I added same code to Page_load and still no luck. My button shows inside a modal so maybe that is the issue. – narue1992 Mar 19 '18 at 15:07
  • So I used your example plus https://stackoverflow.com/questions/24604297/dynamic-created-buttons-vb-net-with-loop to fix my code to work. – narue1992 Mar 19 '18 at 15:28
  • Awesome @narue1992! – hardkoded Mar 19 '18 at 15:42
0

1) Try change that button to an ASP:Button object and not an HTML button.

2) If that doesn't work, change view from code to design view, then double click the button and Visual Studio should automatically create the method (or in your case, direct you to the method where you'll execute code for the button click.)

3) If that doesn't work, drag and drop a button from toolbar and double click and repeat step 2. <-This should definitely work

  • only step 3 would work but isn't what i need. I need to create 'x' amount of buttons based on a page click event that tells me how many to create. They have to be programmatically created versus 'dropped' into the design view. I have attempted `Public btn as new Htmlbutton` and `Public btn as new Button` which is both html type and asp button type – narue1992 Mar 19 '18 at 15:14