We help IT Professionals succeed at work.

Button at Runtime in ASP.NET

Mayank_Agarwal
on
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

Comment
Watch Question

Project manager
CERTIFIED EXPERT
Commented:
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

Commented:
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.

Author

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

Author

Commented:
I am using AJAX as well the placeholder is inside updatepanel

Explore More ContentExplore courses, solutions, and other research materials related to this topic.