473,412 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Display UserControl declared in another Class

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.Question ControlObj)". 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.QuestionC ontrol)
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
Nov 18 '05 #1
2 1292
Se**************@gmx.de (Sebastian Hiller) wrote in message news:<d8**************************@posting.google. com>...
[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.QuestionC ontrol)
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.QuestionC ontrol)
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
Nov 18 '05 #2
Se**************@gmx.de (Sebastian Hiller) wrote in message news:<d8**************************@posting.google. com>...
[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.QuestionC ontrol)
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.QuestionC ontrol)
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
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ZhangZQ | last post by:
Supposing I have a class "UserControl1 : System.Windows.Forms.UserControl" in an Assembly "UI.dll", now I create an Application project and want to use the "UserControl1" in another AppDomain, ...
1
by: jralford | last post by:
Hi, Another question from a C# newbie: I have a usercontrol class that includes a customized control. Now, if I create an instance of the class, and click on it, a click event is raised for the...
3
by: Raj Chudasama | last post by:
i have a custom control derived from usercontrol (called popupwin). I have another usercontrol (called extensionbutton). In the extension button i have declared a variable as follows: private...
0
by: Sebastian Hiller | last post by:
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...
2
by: Tim::.. | last post by:
Can some one please tell me why I keep getting an error saying user has not been declared when I add the following to a class usercontrol. User.Identity.Name If it is the page class then there...
0
by: N. Demos | last post by:
Hello, I have a custom usercontrol, of which I have two instances of in my aspx page. Both the usercontrol and page have codebehind. In the page codebehind, I want a member variable for each...
9
by: Richard Brown | last post by:
Can anyone give me a good argument one way or another? I have an 'address' set of fields that are used in various situations (a client has an address, a destination has an address, etc). These...
5
by: Jim Hubbard | last post by:
I have created a simple usercontrol that adds functionality to the webbrowser control (let's call it ctrl1). I would like to add it to another usercontrol I am creating (let's call it ctrl2), but...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
2
by: marfi95 | last post by:
I am trying to setup a usercontrol as a base class, so that I can derive my usercontrols from it. the base is pretty simple: Imports System.Xml Public Class usrBase Inherits...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.