Hello,
i'm new to .Net (i'm using VB as language and i'm working in the
code-behind mode) and i can't solve the following problem:
I have a WebForm and want to Add a UserControl
(classname:Ques tionControl) as many times as there are rows in a
DataTable (also named Questions) in a DataSet. But this UserControl is
,for reasons of structuring, not a member of the WebForm Object in
which it should be displayed, it is member of another class
(classname:ques tion) along with other Information and forms a logical
unit.
So i'm creating an object of that QuestionControl in the
question-class and add it to a Placeholder in the Webform with
"Me.PlaceHolder 1.Controls.Add( QuestionObj.Que stionControlObj )". But
nothing appears on the WebForm.
I've tried it for test-purposes with a Label-Control instead of the
UserControl and that works fine.
Here is a part of the source:
[WebForm1.aspx.v b]
....
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
Dim QuestionObj As Question
Dim QuestionRow As Dataset2.Questi onRow
Dim QC As New QuestionControl
QuestionCollect ion = New QuestionCollect ion 'A simple
collection for the Question-Class-Objects
For Each QuestionRow In Dataset1.Questi on
QuestionObj = New Question(Questi onRow)
QuestionCollect ion.Add(Questio nObj)
Next
For Each QuestionObj In QuestionCollect ion
Me.PlaceHolder1 .Controls.Add(Q uestionObj.Ques tionControl)
End Sub
....
[QuestionClass.v b]
Public Class Question
Public QuestionControl As New QuestionControl
Dim QuestionID As Integer
'...
Public Sub New(ByRef Question As Dataset2.Questi onRow)
QuestionID = Question.Questi on_ID
QuestionControl .QuestionNr.Tex t = Question.Questi on_Nr
QuestionControl .QuestionNr.Css Class = "QuestionNr "
QuestionControl .QuestionText.T ext = Question.Questi onText
QuestionControl .QuestionText.C ssClass = "QuestionTe xt"
QuestionControl .QuestionHinwei s.Text = Question.Hinwei s
QuestionControl .QuestionHinwei s.CssClass = "QuestionHinwei s"
'...
End Sub
'...
End Class
[QuestionControl .ascx.vb]
Public Class QuestionControl
Inherits System.Web.UI.U serControl
#Region
Public Awnser As System.Web.UI.W ebControls.Plac eHolder
Public QuestionNr As System.Web.UI.W ebControls.Labe l
Public QuestionText As System.Web.UI.W ebControls.Labe l
Public QuestionHinweis As System.Web.UI.W ebControls.Labe l
'Everytime i save that file the Public of the above changes to
"Protected WithEvents"
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
InitializeCompo nent()
End Sub
Public Sub New()
Awnser = New System.Web.UI.W ebControls.Plac eHolder
QuestionNr = New System.Web.UI.W ebControls.Labe l
QuestionText = New System.Web.UI.W ebControls.Labe l
QuestionHinweis = New System.Web.UI.W ebControls.Labe l
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
End Sub
End Class
[QuestionControl .ascx]
<%@ Control Language="vb" AutoEventWireup ="false"
Codebehind="Fra geControl.ascx. vb" Inherits="umfra ge.FrageControl "
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<DIV style="WIDTH: 100%; POSITION: relative; HEIGHT: 46px"
ms_positioning= "GridLayout"><a sp:label id="FrageNr" style="Z-INDEX:
101; LEFT: 16px; POSITION: absolute; TOP: 16px"
BackColor="Tran sparent"
CssClass="Quest ionNr" runat="server"> FrageNr</asp:label><asp: label
id="FrageText" style="Z-INDEX: 102; LEFT: 104px; POSITION: absolute;
TOP: 16px"
CssClass="Quest ionText" Width="80%" runat="server"
BorderColor="Wh ite">FrageText </asp:label><asp: label id="FrageHinwei s"
style="Z-INDEX: 103; LEFT: 488px; POSITION: absolute; TOP: 16px"
CssClass="Quest ionHinweis" Width="20%"
runat="server"> FrageHinweis</asp:label></DIV>
<asp:placeholde r id="Awnser" runat="server"> </asp:placeholder >
Ok, I hope somebody can help me. Please.
Thanks in advance!
Sebastian Hiller