By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,288 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,288 IT Pros & Developers. It's quick & easy.

returning values from dynamically created user control

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Hi Dunc,

Check out the MSDN article:
http://msdn.microsoft.com/library/de...nlifecycle.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
Nov 17 '05 #2

P: n/a
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" <tl********@west.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi Dunc,

Check out the MSDN article:
http://msdn.microsoft.com/library/de...nlifecycle.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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.