Adding Dynamic Controls to Custom Web control

Discussion in 'ASP .Net Web Controls' started by Tipo70, Feb 25, 2008.

  1. Tipo70

    Tipo70 Guest

    I am having problems after adding dynamic controls in a custom web control.
    I have no problem adding the controls. However in the example below, when
    someone clicks on the button, the data in the previous textbox is gone. Is
    there any way I can get this data to stay.

    <ToolboxData("<{0}:TestWebControl runat=server></{0}:TestWebControl>")> _
    Public Class TestWebControl
    Inherits WebControl
    Implements INamingContainer
    Private btnAdd As New Button

    Public Sub New()
    AddHandler btnAdd.Click, AddressOf btnSubmit_Click
    End Sub

    Protected Overrides Sub CreateChildControls()
    Controls.Clear()
    btnAdd.ID = "btnAdd"
    btnAdd.Text = "Add Box"
    If numBoxes = 0 Then numBoxes = 1
    For i As Integer = 1 To numBoxes
    Dim myTextBox As New TextBox()
    myTextBox.ID = "myTextBox" & i
    myTextBox.Width = 200
    Controls.Add(myTextBox)
    Controls.Add(New LiteralControl("<br>"))
    Next
    Controls.Add(New LiteralControl("<br>"))
    Controls.Add(btnAdd)
    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
    EventArgs)
    numBoxes = numBoxes + 1
    Me.ChildControlsCreated = False
    End Sub
    Public Property numBoxes() As Integer
    Get
    Me.EnsureChildControls()
    Return ViewState("numBoxes")
    End Get
    Set(ByVal value As Integer)
    Me.EnsureChildControls()
    ViewState("numBoxes") = value
    End Set
    End Property
    End Class


    Thanks in advance
    Tipo70, Feb 25, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    2
    Views:
    375
    Brian Henry
    Nov 14, 2004
  2. Web Team @ Borough of Poole

    Adding a control to a custom web control

    Web Team @ Borough of Poole, Sep 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    311
    Web Team @ Borough of Poole
    Sep 10, 2005
  3. Ronald
    Replies:
    2
    Views:
    811
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. Tipo70

    Problems Adding Dynamic Controls to Custom Web control

    Tipo70, Feb 27, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    752
    Tipo70
    Feb 27, 2008
  5. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    450
    Eidolon
    May 4, 2004
Loading...

Share This Page