• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2038
  • Last Modified:

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

0
Mayank_Agarwal
Asked:
Mayank_Agarwal
  • 2
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now