returning values from dynamically created user control

Discussion in 'ASP .Net' started by Duncan Welch, Jul 2, 2003.

  1. Duncan Welch

    Duncan Welch Guest

    Hi,

    I've got a fairly simple date control, that I'm creating dynamically in my
    page. The reason for creating it dynamically is that I want the ID to vary,
    depending on it's situation.

    The control loads on the page, and the set date is shown. The problem is,
    when I submit the page, the control doesn't return the date. What am I
    doing wrong?

    Dunc

    ---/ snip /---

    *** extract from default.aspx
    ' Dynamically create start date box
    ctrlStartDate = Me.LoadControl("~\UserControls\DateBox.ascx")
    ctrlStartDate.ControlName = "StartDate"

    ' Add the dynamic controls to their appropriate panels on the page
    pnlStartDate.Controls.Add(ctrlStartDate)

    If Page.IsPostBack Then
    litDateFrom.Text = ctrlStartDate.currDate.ToString
    Else
    ctrlStartDate.currDate = Now
    End If

    ***datebox.ascx
    <asp:panel id="pnlDateControl" runat="server"/>

    ***datebox.ascx.vb
    Public Class DateBox
    Inherits System.Web.UI.UserControl

    Protected WithEvents pnlDateControl As System.Web.UI.WebControls.Panel
    Private m_ControlName As String
    Private m_CurrDate As Date

    WriteOnly Property ControlName() As String
    Set(ByVal Value As String)
    m_ControlName = Value
    End Set
    End Property

    Property currDate() As Date
    Get
    Return m_CurrDate
    End Get
    Set(ByVal Value As Date)
    m_CurrDate = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'build the boxes
    Dim txtDay As New TextBox
    Dim txtMonth As New DropDownList
    Dim txtYear As New TextBox
    Dim liMonth As ListItem
    Dim iLoop As Integer

    txtDay.ID = m_ControlName & "_Day"
    txtDay.CssClass = "inputBox25"
    txtDay.MaxLength = 2
    txtDay.Text = m_CurrDate.Day

    txtMonth.ID = m_ControlName & "_Month"
    txtMonth.CssClass = "inputBox"
    For iLoop = 1 To 12
    liMonth = New ListItem
    liMonth.Value = iLoop
    liMonth.Text = Left(MonthName(iLoop), 3)
    If m_CurrDate.Month = iLoop Then liMonth.Selected = True
    txtMonth.Items.Add(liMonth)
    Next

    txtYear.ID = m_ControlName & "_Year"
    txtYear.CssClass = "inputBox50"
    txtYear.MaxLength = 4
    txtYear.Text = m_CurrDate.Year
    pnlDateControl.Controls.Add(txtDay)
    pnlDateControl.Controls.Add(txtMonth)
    pnlDateControl.Controls.Add(txtYear)
    End Sub
    End Class
    Duncan Welch, Jul 2, 2003
    #1
    1. Advertising

  2. Hi Dunc,

    Check out the MSDN article:
    http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp

    The "Load" event occurs after the "Load ViewState" and "Process Postback"
    events. Since the control doesn't exist yet anything from the previous page
    gets lost. If you want to add a dynamic control and have it work you should
    add it and its event handlers and such during the "Initialize" event.

    Todd Thompson
    Todd Thompson, Jul 2, 2003
    #2
    1. Advertising

  3. Duncan Welch

    Duncan Welch Guest

    Hi Todd,

    Thanks for your feedback. I'm getting further, but still no cigar. Do you
    know where I might find some example code?

    Dunc

    "Todd Thompson" <> wrote in message
    news:...
    > Hi Dunc,
    >
    > Check out the MSDN article:
    >

    http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp
    >
    > The "Load" event occurs after the "Load ViewState" and "Process Postback"
    > events. Since the control doesn't exist yet anything from the previous

    page
    > gets lost. If you want to add a dynamic control and have it work you

    should
    > add it and its event handlers and such during the "Initialize" event.
    >
    > Todd Thompson
    Duncan Welch, Jul 2, 2003
    #3
    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. =?Utf-8?B?SmVu?=

    Getting values from dynamically created control.

    =?Utf-8?B?SmVu?=, Apr 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    508
    =?Utf-8?B?SmVu?=
    Apr 30, 2004
  2. Karthick Kumar
    Replies:
    1
    Views:
    783
  3. Guest
    Replies:
    2
    Views:
    454
    Guest
    Oct 1, 2006
  4. msimmons
    Replies:
    0
    Views:
    460
    msimmons
    Jul 16, 2009
  5. Karthick Kumar

    Get values of dynamically created radio button control

    Karthick Kumar, Nov 5, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    159
    Karthick Kumar
    Nov 5, 2004
Loading...

Share This Page