returning values from dynamically created user control

  • Thread starter Thread starter Duncan Welch
  • Start date Start date
D

Duncan Welch

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
 
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
 
Back
Top