Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Dynamic Controls requires Dynamic event handling? [modified]

Dynamic Controls requires Dynamic event handling? [modified]

Scheduled Pinned Locked Moved Visual Basic
questioncsharpasp-nethelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bjorn T J M Spruit
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • B Bjorn T J M Spruit

      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.

      M Offline
      M Offline
      M Hall
      wrote on last edited by
      #2

      Create your calender control using withevents, create a sub with the same signature as the selection changed event, use addhandler controlname.eventname**, addressof** yourEvent

      B 1 Reply Last reply
      0
      • M M Hall

        Create your calender control using withevents, create a sub with the same signature as the selection changed event, use addhandler controlname.eventname**, addressof** yourEvent

        B Offline
        B Offline
        Bjorn T J M Spruit
        wrote on last edited by
        #3

        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?

        T 1 Reply Last reply
        0
        • B Bjorn T J M Spruit

          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?

          T Offline
          T Offline
          TwoFaced
          wrote on last edited by
          #4

          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.

          B 1 Reply Last reply
          0
          • T TwoFaced

            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.

            B Offline
            B Offline
            Bjorn T J M Spruit
            wrote on last edited by
            #5

            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,

            T 1 Reply Last reply
            0
            • B Bjorn T J M Spruit

              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,

              T Offline
              T Offline
              TwoFaced
              wrote on last edited by
              #6

              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.

              B 1 Reply Last reply
              0
              • T TwoFaced

                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.

                B Offline
                B Offline
                Bjorn T J M Spruit
                wrote on last edited by
                #7

                You guys are AWESOME... Total team effort. I thank M-Hall and Twofaced for their assistance.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups