Declare a User Control in another class

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: with
"Me.PlaceHolder1.Controls.Add(QuestionObj.LabelObj)" appears the
Label.Text on the WebForm.

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
 
I

intrader

Sebastian said:
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: with
"Me.PlaceHolder1.Controls.Add(QuestionObj.LabelObj)" appears the
Label.Text on the WebForm.

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
Perhaps a problem with rendering in your second class?
 
S

Sebastian Hiller

intrader said:
Perhaps a problem with rendering in your second class?

I can't figure out what exactly the problem is, but I tried a lot of
things and the "best" result was that:

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

With this I get an output, but I still hope there is a way to use a
UserControl like a normal control, because with my current solution
I'm not able to put a second UserControl "Answer" in the first
UserControl "Question" (the Answers depend on the Question ID, I'm
trying to make a database generated Questionnaire).

Thanks a lot!
S. Hiller


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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top