Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

layered usercontrols

Status
Not open for further replies.

gagz

Programmer
Nov 21, 2002
333
US
Hi-

I have a page that uses a usercontrol. The usercontrol (1) itself uses a usercontrol (2). This all works fine. The issue is that the user has the ability to dynamically add usercontrol(1) as many as needed. The code to that actually does it is below... what's happening is that it addes the control, but the usercontrol (2) thats included in the usercontrol(1) being added doesn't show.

Could I have used the word user any more?

Code:
Sub AddRoom_Click(Sender As Object, e As System.EventArgs)
    Dim x As Integer
    Dim numRooms As Integer
            
    If NumBORooms.Text <> &quot;&quot; Then
      numRooms = CInt(NumBORooms.Text)
    End If
            
    For x = 1 to numRooms
      Dim c1 As System.Web.UI.UserControl
      c1 = LoadControl(&quot;/bmc/Library/BreakOutRooms.ascx&quot;)
      c1.ID = &quot;BORooms&quot; & (x+1)
      BreakOutRooms.Controls.Add(c1)
    Next x
End Sub

I hope this isn't too confusing...

Thanks,
Marc
 
Could you post the code of usercontrol(1) that loads up usercontrol(2)? I think this is the most likely spot that is crapping out. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
ok here goes... the usercontrol that is being loaded into the main aspx page is:

Code:
<%@ Control Language=&quot;VB&quot; %>
<%@ Register TagPrefix=&quot;Times&quot; TagName=&quot;Times&quot; Src=&quot;/bmc/Library/times.ascx&quot; %>
<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;650&quot; border=&quot;0&quot;>
    <tbody>
        <tr>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                Start</td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                End</td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                Setup Type</td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                No. of<br />
                Delegates</td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                No. of<br />
                days</td>
        </tr>
        <tr>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                <Times:Times id=&quot;BOStartTime&quot; runat=&quot;server&quot;></Times:Times>
            </td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                <Times:Times id=&quot;BOEndTime&quot; runat=&quot;server&quot;></Times:Times>
            </td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                <asp:DropDownList id=&quot;lstBORoomLayout&quot; runat=&quot;server&quot;>
                    <asp:ListItem value=&quot;&quot; selected=&quot;true&quot;>Room Layout</asp:ListItem>
                </asp:DropDownList>
            </td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                <asp:Textbox id=&quot;txtBONumDelegates&quot; runat=&quot;server&quot; size=&quot;3&quot;></asp:Textbox>
            </td>
            <td align=&quot;middle&quot; height=&quot;30&quot;>
                <asp:Textbox id=&quot;txtBONumDays&quot; runat=&quot;server&quot; size=&quot;3&quot;></asp:Textbox>
            </td>
        </tr>
    </tbody>
</table>
<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;650&quot; border=&quot;0&quot;>
    <tbody>
        <tr>
            <td align=&quot;middle&quot; colspan=&quot;6&quot;>
                <asp:CheckBoxList id=&quot;chkBreakOutFood&quot; runat=&quot;server&quot; BackColor=&quot;Transparent&quot; textalign=&quot;Right&quot; repeatlayout=&quot;table&quot; repeatdirection=&quot;Vertical&quot; repeatcolumns=&quot;4&quot; cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;>
                    <asp:ListItem Value=&quot;Breakfast&quot;>Breakfast</asp:ListItem>
                    <asp:ListItem Value=&quot;Lunch&quot;>Lunch</asp:ListItem>
                    <asp:ListItem Value=&quot;Reception&quot;>Reception</asp:ListItem>
                    <asp:ListItem Value=&quot;Morning Coffee&quot;>Morning Coffee</asp:ListItem>
                    <asp:ListItem Value=&quot;Afternoon Tea/COffee&quot;>Afternoon Tea/Coffee</asp:ListItem>
                    <asp:ListItem Value=&quot;Dinner&quot;>Dinner</asp:ListItem>
                </asp:CheckBoxList>
            </td>
        </tr>
    </tbody>
</table>

is just html so to speak with an empty codebehind

the user control that it loads within itself is:
(note its just the codebehind, as the apcx page just had an empty dropdownlist.

Code:
' times.ascx.vb
'

Imports System
Imports System.Collections
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls

    Public Class times 
    
        Inherits UserControl
        
       Public lstTime As DropDownList
    
        Sub Page_Load(sender As Object, e As EventArgs)
            If Not Page.IsPostBack Then
                
                Dim arrTimes as New ArrayList
                Dim strLine as String
                
                Dim FILENAME as String = Server.MapPath(&quot;/bmc/Library/times.inc&quot;)
                
                Dim objStreamReader as StreamReader
                objStreamReader = File.OpenText(FILENAME)

                While (objStreamReader.Peek() <> -1)
                    strLine = objStreamReader.ReadLine()
                    arrTimes.Add(strLine)
                End While
    
                objStreamReader.Close()
                
                lstTime.DataSource = arrTimes
                lstTime.DataBind()

                'create default and set it
                Dim DefItem as New ListItem(&quot;-&quot;,&quot;&quot;)
                lstTime.Items.Insert(0,DefItem)
                
            End If
        End Sub

        Public readonly Property SelectedValue() as String
            Get
                SelectedValue= lstTime.SelectedItem.Value
            End Get
        End Property

    End Class

Thats it... hope it helps.
 
Marc sorry I've been crazy busy today so I just looked over your post. I am going to have to do little testing of my own. I know there is no problem with nested controls but I am think your problem has something to do with nesting your control within itself. I'll check it out and get back to ya That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks Mark... no real hurry, I just appreciate the help!
 
Hey Marc I finally got around to playing with this. From what I can tell initially is that it isn't possible to use a user control within itself. There isn't any problem with different user control within a user control but I can't get the same one to load up. If I find any proof one way or the other I'll be sure to post it. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
i may just be confusing myself...

I'm loading usercontrol1 into my aspx page. usercontrol1 contains usercontrol2 ( a different control). i'm also letting the user load separate instances or usercontrol1 (which contains usercontrol2) as many times as they need.

the only caveat is that usercontrol2 is loaded into each usercontrol1 two times, but with different names...

so does that qualify as loading the same control into itself?

sorry [upsidedown]
 
Oh ok I competely missunderstood you. I thought you were trying to load myWebControl.ascx into myWebControl.ascx.

I did a quick test here I made these three files
WebForm2.aspx
WebUserControl1.ascx
WebUserControl2.ascx

I drug 2 of WebUserControl2.ascx onto WebUserControl1.ascx

Then I used this code to dynamically load x number of WebUserControl1.ascx into WebForm2.aspx


Dim i As Integer

For i = 0 To CInt(txtUserCtrls.Text)
Dim myCntrl As WebTEster.WebUserControl1

myCntrl = LoadControl(&quot;WebUserControl1.ascx&quot;)

myCntrl.ID = &quot;MyControl&quot; & i + 1
pnlUserCtrls.Controls.Add(myCntrl)
pnlUserCtrls.Controls.Add(New LiteralControl(&quot;<BR><HR>&quot;))

myCntrl = Nothing
Next


Hope that answers your question. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
ok posted kinda pathetically...

first, I should not have said that the control doesn't show, but that the control was a dropdown box that didn't appear to be binding... i.e., it was empty.

when i re read my original post thats what clued me in... the section of the usercontrol that filled the drop down box was in a if not postback statement, so whent he user clicked to add the controls, it didn't bind.

so thats fixed now, but i'm wondering if i'll lose viewstate now that I rebind everytime..

hmph. [dazed]
 
Not sure let me know how it goes. Currently I am not to happy with user controls. They don't work near as nicely as I would like them to. However, I think this is simply because I am not building them correctly. IE. Anything you want persisted must be coded into the viewstate manually. As well, they seem to dissapear occasionally from your page.

I am sure that I just need to read up on how things work a little is all. But like I said let me know how things go I am interested. Thanks That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top