[ASP.NET] Dynamisch Controls maken

Pagina: 1
Acties:

  • elmer25
  • Registratie: Februari 2002
  • Laatst online: 01-12-2021

elmer25

ooit was ik 25

Topicstarter
Ik probeer een stukje code te maken waarmee ik dynamisch objecten kan maken en deze objecten kan toevoegen aan de Controls collection van mijn pagina. Het eerste stuk van het probleem heb ik wel opgelost -het maken van de objecten-, alleen heb ik dan een object van type Object. En die kan ik niet met Page.Controls.Add(Object) aan de Controls collection toevoegen.

Ook het omzetten van het object naar type Control (met CType of DirectCast) geeft helaas een error (Invalid cast from System.RuntimeType to Control)

Wie o wie helps mij verder....?!

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
' deze string moet straks uit een database komen:
Dim newControl As String = "System.Web.UI.WebControls.TextBox"

Dim a As Reflection.Assembly = GetType(Control).Module.Assembly
Dim obj As Object
Dim c As ConstructorInfo
Dim pif() As ParameterInfo
Dim args() As Type = {}

obj = a.GetType(newControl, True)

c = obj.GetConstructor(BindingFlags.Instance Or BindingFlags.CreateInstance Or _
 BindingFlags.Public, Nothing, _
 CallingConventions.HasThis, args, Nothing)

pif = c.GetParameters

c.Invoke(pif)

' en hier komt de fout: Invalid cast...
Me.Controls.Add(obj)

  • maikel
  • Registratie: Januari 2001
  • Laatst online: 13:52
Van welk type leidt je je control af ?
Het is volgens mij het makkelijkst als je die van (User)Control afleidt.

Wat moet het precies doen ?

  • elmer25
  • Registratie: Februari 2002
  • Laatst online: 01-12-2021

elmer25

ooit was ik 25

Topicstarter
Ik leid het control van niks af. Ik wil een string ui t de database lezen en afhankelijk van de waarde een control maken. En eventueel kan dat wel met een Select-Case constructie, maar ik wil verderop in het programma ook de properties van het control dynamisch zetten (bijv. TextBox.Text, TextBox.BackGroundColor, etc) dus ik moet echt de control als objecten hebben.

  • maikel
  • Registratie: Januari 2001
  • Laatst online: 13:52
Die waarde moet als tekst in een TextBox komen ??

  • elmer25
  • Registratie: Februari 2002
  • Laatst online: 01-12-2021

elmer25

ooit was ik 25

Topicstarter
Ik lees een string-waarde uit de database, bijv. "TextBox". Op dat moment wil ik dus een nieuw TextBox-control maken. Vervolgens lees ik uit de database "Text=waarde" en dan moet de property Text van de TextBox de waarde "waarde" krijgen. Maar het geheel moet zo generiek zijn dat de TextBox ook een Label kan zijn, of een willekeurig ander control met andere properties en dan moet het programma gewoon doorlopen

Verwijderd

eh...
Ik mis ergens

Dim newobject as New Object()

  • maikel
  • Registratie: Januari 2001
  • Laatst online: 13:52
elmer25 schreef op 02 december 2002 @ 11:44:
Ik lees een string-waarde uit de database, bijv. "TextBox". Op dat moment wil ik dus een nieuw TextBox-control maken. Vervolgens lees ik uit de database "Text=waarde" en dan moet de property Text van de TextBox de waarde "waarde" krijgen. Maar het geheel moet zo generiek zijn dat de TextBox ook een Label kan zijn, of een willekeurig ander control met andere properties en dan moet het programma gewoon doorlopen
Aha !
Maar dan kun je nog van (User)Control afleiden, daar worden de TextBoxen en Labels ook van afgeleid.
Nu leidt je van Object af en die kan ie niet altijd casten.

  • elmer25
  • Registratie: Februari 2002
  • Laatst online: 01-12-2021

elmer25

ooit was ik 25

Topicstarter
Verwijderd schreef op 02 December 2002 @ 11:49:
eh...
Ik mis ergens

Dim newobject as New Object()
dat is dus precies het stukje wat dynamisch moet gebeuren, want ik weet van te voren niet wat Object is... omdat ik dat als een string-waarde uitlees...

  • elmer25
  • Registratie: Februari 2002
  • Laatst online: 01-12-2021

elmer25

ooit was ik 25

Topicstarter
maikel schreef op 02 december 2002 @ 11:58:
[...]


Aha !
Maar dan kun je nog van (User)Control afleiden, daar worden de TextBoxen en Labels ook van afgeleid.
Nu leidt je van Object af en die kan ie niet altijd casten.
helaas, dan werkt het volgende stukje code niet meer, om een nieuw object aan te maken

code:
1
2
3
4
5
6
obj = a.GetType(newControl, True)

c = obj.GetConstructor(BindingFlags.Instance _
  Or BindingFlags.CreateInstance Or _
  BindingFlags.Public, Nothing, _
  CallingConventions.HasThis, args, Nothing)

Verwijderd

Wat indien je dynamisch een class genereert die op basis van je string de functies eruit poept? Dan krijg je wel een Dim myObject as New <blaat>

Of je gebruikt als een dynamische property en haalt het soort object gewoon uit een xml-bestandje?

Ff denken...


' deze string moet straks uit een database komen:
Dim newControl As String = "System.Web.UI.WebControls.TextBox"

Dim myObject as New Object

.
.
.
myObject =CObj(newControl)

  • maikel
  • Registratie: Januari 2001
  • Laatst online: 13:52
Verwijderd schreef op 02 December 2002 @ 12:13:
Wat indien je dynamisch een class genereert die op basis van je string de functies eruit poept? Dan krijg je wel een Dim myObject as New <blaat>

Of je gebruikt als een dynamische property en haalt het soort object gewoon uit een xml-bestandje?

Ff denken...


' deze string moet straks uit een database komen:
Dim newControl As String = "System.Web.UI.WebControls.TextBox"

Dim myObject as New Object

.
.
.
myObject =CObj(newControl)
Maar op die manier is myObject toch nog steeds van het type Object, of niet ??

En "Dim myObject as New <blaat>" heef toch te maken met generics ??
Die worden nog niet ondersteund in .Net.

Verwijderd

Lees dit ff door, misschien haal je daar wel wat info uit :)
Heb nu even te weinig tijd om er dieper op in te gaan.

Visual Basic .NET:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
' Create a property so the controls can be easily retrieved.
    Public ReadOnly Property FormControls() As Control.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        ' Create the controls we'll be using in all of the later loops.
        Dim myControl As Control
        Dim myGroupControl As Control
        Dim myGroupBox As GroupBox
        Dim myObject As Object
        Dim myRadio As RadioButton

        ' Reset the Response string
        Me.m_FormResponse = ""

        ' Loop through each control and output the user response into the
        '   the FormResponse string. (The string could easily be replaced
        '   with a collection of some sort.)
        For Each myControl In Me.Controls
            ' Differentiate output based on the type of the control
            Select Case TypeName(myControl)
                Case "ComboBox"
                    ' Simple get the text out of the group box and add it to the
                    '   response string, along with the question name
                    m_FormResponse += myControl.Name + " - "
                    m_FormResponse += myControl.Text
                    m_FormResponse += vbCrLf
                Case "TextBox"
                    ' Simple get the text out of the group box and add it to the
                    '   response string, along with the question name
                    m_FormResponse += myControl.Name + " - "
                    m_FormResponse += myControl.Text
                    m_FormResponse += vbCrLf
                Case "GroupBox"
                    ' Need to go inside of the GroupBox to yank out the 
                    '   RadioButtons
                    For Each myGroupControl In CType(myControl, GroupBox).Controls
                        If TypeOf myGroupControl Is RadioButton Then
                            If CType(myGroupControl, RadioButton).Checked Then
                                ' Simple get the question and response of the 
                                '   user being Formed.
                                m_FormResponse += myControl.Name + " - "
                                m_FormResponse += myGroupControl.Text
                                m_FormResponse += vbCrLf
                            End If
                        End If

                    Next
                Case "ListBox"
                    ' For this one we must get each of the selected lines, and 
                    '   return them.
                    m_FormResponse += myControl.Name + " - "
                    For Each myObject In CType(myControl, ListBox).SelectedItems
                        If TypeOf myObject Is String Then
                            ' Simple get the question and response of the 
                            '   user being Formed.
                            m_FormResponse += vbTab + CStr(myObject)
                            m_FormResponse += vbCrLf
                        End If
                    Next
            End Select
        Next

        ' Close the form.
        Me.Close()
    End Sub

  • maikel
  • Registratie: Januari 2001
  • Laatst online: 13:52
Die ken ik ja, maar dat is een hoop gedoe om te installeren.
Eerst die Gyro downloaden, die bleek weer Rotor nodig te hebben. Maar die had weer ActivePerl nodig. En die moest weer C++ geinstalleerd hebben.
En daar had ik geen zin meer in, dus toen heb ik het maar allemaal weer verwijderd. :)

Maar het zit er dus standaard nog niet in.

  • elmer25
  • Registratie: Februari 2002
  • Laatst online: 01-12-2021

elmer25

ooit was ik 25

Topicstarter
Dim myObject as New Object

.
.
.
myObject =CObj(newControl)
dan heb je dus een object van type String, en ook die kun je helaas niet aan een Controls collectie toevoegen...

  • mulder
  • Registratie: Augustus 2001
  • Laatst online: 00:27

mulder

ik spuug op het trottoir

denk dat je misschien eerst obj = c.invoke(pif); moet doen.

oogjes open, snaveltjes dicht

Pagina: 1