473,395 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Nov 17 '05 #1
2 4267
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jen | last post by:
Hi I have created some controls (HtmlInputText, HtmlGenericControl, TextBox) dynamically. But I have problem getting the values from these controls to save them. Is there a way to do that? ...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
9
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: jburnage | last post by:
I need help in getting the values from a dynamically added user control when a user submits a form. The controls are added to a placeholder and this all works fine - but its looping through the...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.