Display UserControl declared in another Class

  • Thread starter Sebastian Hiller
  • Start date
S

Sebastian Hiller

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:QuestionControl) 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:question) 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.PlaceHolder1.Controls.Add(QuestionObj.QuestionControlObj)". 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.vb]
....
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

Dim QuestionObj As Question
Dim QuestionRow As Dataset2.QuestionRow
Dim QC As New QuestionControl
QuestionCollection = New QuestionCollection 'A simple
collection for the Question-Class-Objects
For Each QuestionRow In Dataset1.Question
QuestionObj = New Question(QuestionRow)
QuestionCollection.Add(QuestionObj)
Next
For Each QuestionObj In QuestionCollection

Me.PlaceHolder1.Controls.Add(QuestionObj.QuestionControl)
End Sub
....

[QuestionClass.vb]
Public Class Question
Public QuestionControl As New QuestionControl
Dim QuestionID As Integer
'...
Public Sub New(ByRef Question As Dataset2.QuestionRow)
QuestionID = Question.Question_ID
QuestionControl.QuestionNr.Text = Question.Question_Nr
QuestionControl.QuestionNr.CssClass = "QuestionNr"
QuestionControl.QuestionText.Text = Question.QuestionText
QuestionControl.QuestionText.CssClass = "QuestionText"
QuestionControl.QuestionHinweis.Text = Question.Hinweis
QuestionControl.QuestionHinweis.CssClass = "QuestionHinweis"
'...
End Sub
'...
End Class

[QuestionControl.ascx.vb]
Public Class QuestionControl
Inherits System.Web.UI.UserControl

#Region
Public Awnser As System.Web.UI.WebControls.PlaceHolder
Public QuestionNr As System.Web.UI.WebControls.Label
Public QuestionText As System.Web.UI.WebControls.Label
Public QuestionHinweis As System.Web.UI.WebControls.Label

'Everytime i save that file the Public of the above changes to
"Protected WithEvents"

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
Public Sub New()
Awnser = New System.Web.UI.WebControls.PlaceHolder
QuestionNr = New System.Web.UI.WebControls.Label
QuestionText = New System.Web.UI.WebControls.Label
QuestionHinweis = New System.Web.UI.WebControls.Label
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub

End Class

[QuestionControl.ascx]
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="FrageControl.ascx.vb" Inherits="umfrage.FrageControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<DIV style="WIDTH: 100%; POSITION: relative; HEIGHT: 46px"
ms_positioning="GridLayout"><asp:label id="FrageNr" style="Z-INDEX:
101; LEFT: 16px; POSITION: absolute; TOP: 16px"
BackColor="Transparent"
CssClass="QuestionNr" runat="server">FrageNr</asp:label><asp:label
id="FrageText" style="Z-INDEX: 102; LEFT: 104px; POSITION: absolute;
TOP: 16px"
CssClass="QuestionText" Width="80%" runat="server"
BorderColor="White">FrageText</asp:label><asp:label id="FrageHinweis"
style="Z-INDEX: 103; LEFT: 488px; POSITION: absolute; TOP: 16px"
CssClass="QuestionHinweis" Width="20%"
runat="server">FrageHinweis</asp:label></DIV>
<asp:placeholder id="Awnser" runat="server"></asp:placeholder>

Ok, I hope somebody can help me. Please.
Thanks in advance!

Sebastian Hiller
 
S

Sebastian Hiller

[WebForm1.aspx.vb]
...
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

Dim QuestionObj As Question
Dim QuestionRow As Dataset2.QuestionRow
Dim QC As New QuestionControl
QuestionCollection = New QuestionCollection 'A simple
collection for the Question-Class-Objects
For Each QuestionRow In Dataset1.Question
QuestionObj = New Question(QuestionRow)
QuestionCollection.Add(QuestionObj)
Next
For Each QuestionObj In QuestionCollection

Me.PlaceHolder1.Controls.Add(QuestionObj.QuestionControl)
End Sub
...

[QuestionClass.vb]
Public Class Question
Public QuestionControl As New QuestionControl
Dim QuestionID As Integer
'...
Public Sub New(ByRef Question As Dataset2.QuestionRow)
QuestionID = Question.Question_ID
QuestionControl.QuestionNr.Text = Question.Question_Nr
QuestionControl.QuestionNr.CssClass = "QuestionNr"
QuestionControl.QuestionText.Text = Question.QuestionText
QuestionControl.QuestionText.CssClass = "QuestionText"
QuestionControl.QuestionHinweis.Text = Question.Hinweis
QuestionControl.QuestionHinweis.CssClass = "QuestionHinweis"
'...
End Sub
'...
End Class
...

The Following works:
I declare an Array and give an element of that as argument to the
questionclass-constructor. But i hope there is a better solution,
because that looks ugly and i don't want to give an extra argument to
the constructor of the QuestionClass.

[WebForm1.aspc.vb]
Dim QuestionObj As Question
Dim QuestionRow As Dataset2.QuestionRow
Dim i As Integer = 0
Dim c1(100) As QuestionControl 'the Array of
QuestionControls
QuestionCollection = New QuestionCollection
For Each QuestionRow In Dataset1.Question
c1(i) = LoadControl("QuestionControl.ascx")
QuestionObj = New Question(QuestionRow, c1(i))
'QuestionControl as arg
QuestionCollection.Add(QuestionObj)
i = i + 1
Next
For Each QuestionObj In QuestionCollection
Me.PlaceHolder1.Controls.Add(QuestionObj.QuestionControl)
Next

[QuestionClass.vb]
Public Class Question
Public QuestionControl As QuestionControl
Dim QuestionID As Integer
...
Public Sub New(ByRef Question As Dataset2.QuestionRow, ByVal FC As
QuestionControl)
QuestionControl = FC
QuestionID = Question.Question_ID
Dim AntwortRow As Dataset2.InhaltRow
QuestionControl.QuestionNr.Text = Question.Question_Nr
QuestionControl.QuestionNr.CssClass = "QuestionNr"
QuestionControl.QuestionText.Text = Question.QuestionText
QuestionControl.QuestionText.CssClass = "QuestionText"
QuestionControl.QuestionHint.Text = Question.Hint
QuestionControl.QuestionHint.CssClass = "QuestionHint"
...
End Sub
End Class

I think there must be a more common way to do such a simple thing. It
must be possible to use a UserControl in the same way as a standard
WebForm Control, that is what i expect from it (and maybe others too).

With best Regrads,

Sebastian Hiller
 
S

Sebastian Hiller

[WebForm1.aspx.vb]
...
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

Dim QuestionObj As Question
Dim QuestionRow As Dataset2.QuestionRow
Dim QC As New QuestionControl
QuestionCollection = New QuestionCollection 'A simple
collection for the Question-Class-Objects
For Each QuestionRow In Dataset1.Question
QuestionObj = New Question(QuestionRow)
QuestionCollection.Add(QuestionObj)
Next
For Each QuestionObj In QuestionCollection

Me.PlaceHolder1.Controls.Add(QuestionObj.QuestionControl)
End Sub
...

[QuestionClass.vb]
Public Class Question
Public QuestionControl As New QuestionControl
Dim QuestionID As Integer
'...
Public Sub New(ByRef Question As Dataset2.QuestionRow)
QuestionID = Question.Question_ID
QuestionControl.QuestionNr.Text = Question.Question_Nr
QuestionControl.QuestionNr.CssClass = "QuestionNr"
QuestionControl.QuestionText.Text = Question.QuestionText
QuestionControl.QuestionText.CssClass = "QuestionText"
QuestionControl.QuestionHinweis.Text = Question.Hinweis
QuestionControl.QuestionHinweis.CssClass = "QuestionHinweis"
'...
End Sub
'...
End Class
...

The Following works:
I declare an Array and give an element of that as argument to the
questionclass-constructor. But i hope there is a better solution,
because that looks ugly and i don't want to give an extra argument to
the constructor of the QuestionClass.

[WebForm1.aspc.vb]
Dim QuestionObj As Question
Dim QuestionRow As Dataset2.QuestionRow
Dim i As Integer = 0
Dim c1(100) As QuestionControl 'the Array of
QuestionControls
QuestionCollection = New QuestionCollection
For Each QuestionRow In Dataset1.Question
c1(i) = LoadControl("QuestionControl.ascx")
QuestionObj = New Question(QuestionRow, c1(i))
'QuestionControl as arg
QuestionCollection.Add(QuestionObj)
i = i + 1
Next
For Each QuestionObj In QuestionCollection
Me.PlaceHolder1.Controls.Add(QuestionObj.QuestionControl)
Next

[QuestionClass.vb]
Public Class Question
Public QuestionControl As QuestionControl
Dim QuestionID As Integer
...
Public Sub New(ByRef Question As Dataset2.QuestionRow, ByVal FC As
QuestionControl)
QuestionControl = FC
QuestionID = Question.Question_ID
Dim AntwortRow As Dataset2.InhaltRow
QuestionControl.QuestionNr.Text = Question.Question_Nr
QuestionControl.QuestionNr.CssClass = "QuestionNr"
QuestionControl.QuestionText.Text = Question.QuestionText
QuestionControl.QuestionText.CssClass = "QuestionText"
QuestionControl.QuestionHint.Text = Question.Hint
QuestionControl.QuestionHint.CssClass = "QuestionHint"
...
End Sub
End Class

I think there must be a more common way to do such a simple thing. It
must be possible to use a UserControl in the same way as a standard
WebForm Control, that is what i expect from it (and maybe others too).

With best Regrads
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top