Dynamic Controls requires Dynamic event handling? [modified]
-
Hey everyone, I've got a webpage that has a selfmade webusercontrol. That webusercontrol dynamically creates (at this moment only) a label control, a calendar control and a textbox control, depending on how many items I give the control through a Session. So far so good, for every item in Session a set of these controls is created and added to the page. They display nicely. *I hear the collective going "BUT?"* Ah yes, there's a "but". What I want to do, is fill the textbox of a set with the selected date of the calendar object of THAT set. But there are (in this example) two calendar objects. NOTE: (I have to add the calendar objects dynamically as I don't know how many date type parameters will be required in the near future) How can I get the selectionchanged event from one of the calendar, so that the control "knows" it's from that calendar and fill the correct textbox that belongs to that calendar ? Don't forget, all the controls are being generated at runtime in a webusercontrol, which means that somehow I have to create dynamic eventhandlers that "knows" which calendar object had the selectionchanged event and "knows" which which textbox object to set the text of. Code in VB.NET please, not ASP.NET. Everything is being done in the code behind, which is VB.NET. Some help would be greatly appreciated.
-
Hey everyone, I've got a webpage that has a selfmade webusercontrol. That webusercontrol dynamically creates (at this moment only) a label control, a calendar control and a textbox control, depending on how many items I give the control through a Session. So far so good, for every item in Session a set of these controls is created and added to the page. They display nicely. *I hear the collective going "BUT?"* Ah yes, there's a "but". What I want to do, is fill the textbox of a set with the selected date of the calendar object of THAT set. But there are (in this example) two calendar objects. NOTE: (I have to add the calendar objects dynamically as I don't know how many date type parameters will be required in the near future) How can I get the selectionchanged event from one of the calendar, so that the control "knows" it's from that calendar and fill the correct textbox that belongs to that calendar ? Don't forget, all the controls are being generated at runtime in a webusercontrol, which means that somehow I have to create dynamic eventhandlers that "knows" which calendar object had the selectionchanged event and "knows" which which textbox object to set the text of. Code in VB.NET please, not ASP.NET. Everything is being done in the code behind, which is VB.NET. Some help would be greatly appreciated.
-
Create your calender control using withevents, create a sub with the same signature as the selection changed event, use addhandler controlname.eventname**, addressof** yourEvent
If I use a withevents declaration, I end up with only one calendar on the resulting page. Seems as though it overwrites the first one. Perhaps because it uses a global identifier within the control instead of one contained in a sub?
-
If I use a withevents declaration, I end up with only one calendar on the resulting page. Seems as though it overwrites the first one. Perhaps because it uses a global identifier within the control instead of one contained in a sub?
-
You don't need withevents to add an event handler at runtime. Just declare it like you have been but add the handler as suggested when you create each control.
That works like a charm, except... How do I set the dynamic txtCalendar.text if it's not globally declared? How can I find the dynamicaly declared textbox and set the text value?
Code so far:
Public Class ListReportParametersControl Inherits System.Web.UI.UserControl Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim intCounter1 As Integer Dim strParamValue As String Dim arrSessionParams() As String = Session("ReportParams").ToString.Split(",") Dim arrTempTypeNValue() As String For intCounter1 = 0 To UBound(arrSessionParams) arrTempTypeNValue = arrSessionParams(intCounter1).ToString.Split(":") Select Case arrTempTypeNValue(0) Case "CALENDAR" SetCalendar(arrTempTypeNValue(1)) ReDim Preserve arrControls(intCounter1) arrControls(intCounter1) = arrTempTypeNValue(1).ToString End Select Next End Sub Public Sub SetCalendar(ByVal CalendarID As String) Dim lblCalendar As New Label lblCalendar.Font.Name = "Verdana" lblCalendar.Font.Size = FontUnit.Parse("10") lblCalendar.ID = "Label" & CalendarID lblCalendar.Text = CalendarID Dim myCalendar As New Calendar myCalendar.Font.Name = "Verdana" myCalendar.Font.Size = FontUnit.Parse("10") myCalendar.ID = CalendarID myCalendar.EnableViewState = True myCalendar.TodaysDate = System.DateTime.Today myCalendar.VisibleDate = System.DateTime.Today myCalendar.TitleFormat = TitleFormat.MonthYear myCalendar.SelectionMode = CalendarSelectionMode.Day myCalendar.NextPrevFormat = NextPrevFormat.CustomText myCalendar.TodayDayStyle.BackColor = System.Drawing.Color.Red myCalendar.OtherMonthDayStyle.BackColor = System.Drawing.Color.FromName("LightGray") AddHandler myCalendar.SelectionChanged, AddressOf myCalendarSelectionChanged Dim txtCalendar As New TextBox txtCalendar.Font.Name = "Verdana" txtCalendar.Font.Size = FontUnit.Parse("10") txtCalendar.ID = "Textbox" & CalendarID 'txtCalendar.Text = myCalendar.UniqueID.ToString Me.Controls.Add(lblCalendar) Me.Controls.Add(myCalendar) Me.Controls.Add(txtCalendar) End Sub Public Sub myCalendarSelectionChanged(ByVal sender As Object,
-
That works like a charm, except... How do I set the dynamic txtCalendar.text if it's not globally declared? How can I find the dynamicaly declared textbox and set the text value?
Code so far:
Public Class ListReportParametersControl Inherits System.Web.UI.UserControl Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim intCounter1 As Integer Dim strParamValue As String Dim arrSessionParams() As String = Session("ReportParams").ToString.Split(",") Dim arrTempTypeNValue() As String For intCounter1 = 0 To UBound(arrSessionParams) arrTempTypeNValue = arrSessionParams(intCounter1).ToString.Split(":") Select Case arrTempTypeNValue(0) Case "CALENDAR" SetCalendar(arrTempTypeNValue(1)) ReDim Preserve arrControls(intCounter1) arrControls(intCounter1) = arrTempTypeNValue(1).ToString End Select Next End Sub Public Sub SetCalendar(ByVal CalendarID As String) Dim lblCalendar As New Label lblCalendar.Font.Name = "Verdana" lblCalendar.Font.Size = FontUnit.Parse("10") lblCalendar.ID = "Label" & CalendarID lblCalendar.Text = CalendarID Dim myCalendar As New Calendar myCalendar.Font.Name = "Verdana" myCalendar.Font.Size = FontUnit.Parse("10") myCalendar.ID = CalendarID myCalendar.EnableViewState = True myCalendar.TodaysDate = System.DateTime.Today myCalendar.VisibleDate = System.DateTime.Today myCalendar.TitleFormat = TitleFormat.MonthYear myCalendar.SelectionMode = CalendarSelectionMode.Day myCalendar.NextPrevFormat = NextPrevFormat.CustomText myCalendar.TodayDayStyle.BackColor = System.Drawing.Color.Red myCalendar.OtherMonthDayStyle.BackColor = System.Drawing.Color.FromName("LightGray") AddHandler myCalendar.SelectionChanged, AddressOf myCalendarSelectionChanged Dim txtCalendar As New TextBox txtCalendar.Font.Name = "Verdana" txtCalendar.Font.Size = FontUnit.Parse("10") txtCalendar.ID = "Textbox" & CalendarID 'txtCalendar.Text = myCalendar.UniqueID.ToString Me.Controls.Add(lblCalendar) Me.Controls.Add(myCalendar) Me.Controls.Add(txtCalendar) End Sub Public Sub myCalendarSelectionChanged(ByVal sender As Object,
The quickest fix I can think of is a hashtable. Use the calander object as the key and the textbox will be the value. Declare the hashtable globally and when you create a new 'control group' add the new value.
'Declare globally
dim hash as new hashtable'In the SetCalander method
hash.add(myCalendar, txtCalendar)'In the selectionchange event
'This will give you a reference to the appropriate textbox
dim txt as textbox = hash(sender)This should work. It may not be the best solution but it was the quickest fix I could think of.
-
The quickest fix I can think of is a hashtable. Use the calander object as the key and the textbox will be the value. Declare the hashtable globally and when you create a new 'control group' add the new value.
'Declare globally
dim hash as new hashtable'In the SetCalander method
hash.add(myCalendar, txtCalendar)'In the selectionchange event
'This will give you a reference to the appropriate textbox
dim txt as textbox = hash(sender)This should work. It may not be the best solution but it was the quickest fix I could think of.
You guys are AWESOME... Total team effort. I thank M-Hall and Twofaced for their assistance.