Button at Runtime in ASP.NET

Hi Guys
Here is a code snippet from by ASp.NEt page, I want to add a button to the placeholder at runtime, when the user click the button the code should execute.
Obviously there is something wrong its not working. If you can look at it and prove a solution please.

Public Sub createNewSite()
 
        Me.Label1.Text = "This is to create a new site"
        Dim sName As New TextBox
        Dim btnCreate As Button
        Me.plCreateNewSite.Controls.Add(sName)
        AddHandler btnCreate.Click, New System.EventArgs(AddressOf btnCreate_Click)
        Me.plCreateNewSite.Controls.Add(btnCreate)
 
    End Sub
    Public Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
        dsGetSiteNames.InsertParameters.Add("SiteID", 3)
        dsGetSiteNames.InsertParameters.Add("SiteName", "Gold Coast")
        dsGetSiteNames.Insert()
    End Sub

Open in new window

LVL 1
Mayank_AgarwalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
Some minor mistakes in your code
    Public Sub createNewSite()
 
        Me.Label1.Text = "This is to create a new site"
        Dim sName As New TextBox
        Dim btnCreate As New Button
        Me.plCreateNewSite.Controls.Add(sName)
        Me.plCreateNewSite.Controls.Add(btnCreate)
        AddHandler btnCreate.Click, AddressOf btnCreate_Click
        Me.plCreateNewSite.Controls.Add(btnCreate)
 
    End Sub
    Public Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
        dsGetSiteNames.InsertParameters.Add("SiteID", 3)
        dsGetSiteNames.InsertParameters.Add("SiteName", "Gold Coast")
        dsGetSiteNames.Insert()
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobbPCommented:
Hi, couple of things to remember when instantiating a dynamic control, first declare it iwthin your class:

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button

Then within the method you wish to create the button, create a new instance and add it:
    Public Sub createNewSite()
        Button2 = New Button
        Button2.Text = "2"
        form1.Controls.Add(Button2)

    End Sub

And lastly the event, use the UI to create it:
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox("Hello")
    End Sub

Let me know if this is what ur looking for.
0
Mayank_AgarwalAuthor Commented:
Hi Guys thanks for the reply both of you but it doesnt seem to be working.
The have tried to debug, it it doesnt execute the buttone click even, in both the cases. when i put my mouse over the me.plcreatenewsite.controls.add(btnCreate) it should us as (Text="") i would have thought that i will show us as system,web.ui.webcontrols.button
in any case, i might need some more ideas please
0
Mayank_AgarwalAuthor Commented:
I am using AJAX as well the placeholder is inside updatepanel
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.