locked
Dynamic Control RRS feed

  • Question

  • User132775050 posted

    I have a control I wish to dynamically load onto an aspx page, using a helper class.  The control will be loaded onto a Master Page.

    Folder Tree
    ~/ABC/ABCMaster.master
    ~/ABC/ABCMaster.master.vb
    ~/App_Code/Classes/ABCFieldTypes.vb
    ~/ControlsABC/CtlABCCustomerName.aspx  (This control is an asp:DropDownList)
    ~/ControlsABC/CtlABCCustomerName.aspx.vb
     

    Master.vb
    Here I call the function  RenderCriteriaTable 

        Public Sub RenderCriteriaTable(ByVal myArr(,) As String)
            Dim Tbl As HtmlTable = Me.tblCriteriaFields
            Dim index As Integer = 0
    
            For index = 0 To (myArr.Length / 3) - 1
                Dim TblRow As New HtmlTableRow
                TblRow = ABCFieldTypes.RenderRow(myArr(index, 0), myArr(index, 1), myArr(index, 2))
    
                'Create Row with Cells
                Tbl.Rows.Insert((Tbl.Rows.Count - 1), TblRow)
            Next
        End Sub

    ABCFieldTypes.vb
    Here is where I am trying to dynamically load the control 

        Public Shared Function RenderRow(ByVal FieldID As String, ByVal FieldType As Integer, ByVal ShowField As Integer)
    Dim TblRow As New HtmlTableRow
    'Cells 1 and 3 will always be the same

    'Cell 1
    Dim TblCell1 As New HtmlTableCell
    Dim lbl As New Label
    lbl.Text = FieldID
    lbl.ID = "lbl" & FieldID.Replace(" ", "_").ToString
    lbl.Font.Bold = True TblCell1.Controls.Add(lbl) TblRow.Cells.Add(TblCell1) 'Cell 2 Select Case FieldType
    Case FieldTypes.FieldNone
    TblRow.Cells.Add(BlankCell())
    Case FieldTypes.FieldTextBox
    Dim TblCell2 As New HtmlTableCell
    Dim txt As New BDK.WMS.Controls.TextBox.TextBox
    txt.ID = "txt" & FieldID.Replace(" ", "_").ToString
    TblCell2.Controls.Add(txt)
    TblRow.Cells.Add(TblCell2)
    Case FieldTypes.FieldDropDownList
    Dim TblCell2 As New HtmlTableCell
    Dim ddl As New UserControl
    ddl.LoadControl("~/ControlsABC/CtlAbcCustomerName.ascx")
    ddl.ID = "ddl" & FieldID.Replace(" ", "_").ToString
    TblCell2.Controls.Add(ddl)
    TblRow.Cells.Add(TblCell2)

    End Select 'Cell 3 If ShowField = ABCFieldTypes.ShowField.Yes Then
    Dim
    TblCell3 As New HtmlTableCell
    Dim chkbox As New CheckBox
    chkbox.ID = "chk" & FieldID.Replace(" ", "_").ToString
    chkbox.Visible = ShowField
    TblCell3.Controls.Add(chkbox)
    TblCell3.Align = "center" TblRow.Cells.Add(TblCell3) Else TblRow.Cells.Add(BlankCell()) End If

    Return
    TblRow
    End Function
    It seems as if the Control is found, but does not appear on the page.  Any ideas?  I've done some searching and came across something about Interfaces, but could not understand it. If possible I would like to be able to use the properties on the control that I've made public.  I can reference these if I register the control on the page manually.  I need the dynamics because different pages will have different controls.  One other thing, it seems to work if I place the control inside the App_Code directory, but I cannot use this as a solution.
    Thursday, May 29, 2008 12:42 PM

Answers

  • User132775050 posted

    Never I found my answer. 

    Case FieldTypes.FieldDropDownList
         Dim TblCell2 As New HtmlTableCell
    *     Dim p As New Page
    *     Dim ddl As New Control
    *     ddl = p.LoadControl("~/ControlsABC/CtlAbcDay.ascx")
         ddl.ID = "ddl" & FieldID.Replace(" ", "_").ToString
         TblCell2.Controls.Add(ddl)
         TblRow.Cells.Add(TblCell2)
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 29, 2008 1:45 PM