returning values from dynamically created user control

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default returning values from dynamically created user control

    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 Guest

  2. Similar Questions and Discussions

    1. Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created)
      Ok here's my scenario. I have a Custom Composite Server Control (CCSC) consisting of a TextBox, Button & Panel. (And some other code - which I...
    2. saving text values of dynamically created textboxes
      I have many textboxes that are created dynamically as child controls of my custom control. I know that I must recreate them after each postback...
    3. Retrieving Values from dynamically created controls in a user control
      Hi, Dynamic control should be load every time page load, even on post back. take out control creation from IsPostBack condition. Natty Gur ...
    4. retrieving values from dynamically created controls
      Hi, Someone please clear this for me! I display a bunch of button controls on a page dynamically, adding them to a placeholder control in my...
    5. need help with dynamically created user control
      i have a dynamically created user control which contains a non-dynamically created ASP.Net button. When the button is clicked, the event is not...
  3. #2

    Default Re: returning values from dynamically created user control

    Hi Dunc,

    Check out the MSDN article:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp[/url]

    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 Guest

  4. #3

    Default Re: returning values from dynamically created user control

    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" <tlthompson@west.com> wrote in message
    news:eaAItHKQDHA.2432@TK2MSFTNGP10.phx.gbl...
    > Hi Dunc,
    >
    > Check out the MSDN article:
    >
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp[/url]
    >
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139