Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?

 Sub createbutton()

        Dim but As New Button
        but.Text = "save"
        but.ID = "but"
        AddHandler but.Click, AddressOf Button_Click
        Me.form1.Controls.Add(but)

    End Sub

The event control for this is as below.

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' Handle your Button clicks here
        MsgBox("done")
    End Sub

Im getting the error

Multiple controls with the same ID '1' were found

The subroutine createbutton works on page load as follows.

Public Class Default3

    Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
    createbutton()

End Sub

Help is appreciated , Thanks :)

Related posts

Recent Viewed