• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1524
  • Last Modified:

call asp.net usercontrol form asp.net usercontrol

Hi
I am just starting a new application (asp.net/vb.net) and was wondering if I could do it in a different way as I usually do: Instead of the user creating dozens of pages I would like them to see only a default.aspx with dynamically loaded UserControls.

Thing is I have 3 distintive areas on my page: A top menu, a side menu and the content area.

My question is the following; Can I call a UserControl from another UserControl?

EG Clicking on the top menu (ascx) would load a sidebar menu (another ascx), clicking on the sidebar would open another ascx in the content area.

Is this a feasable option?

I seem to rember seeing applications whre the user always stayed on the default.aspx.

Thanks, Mike
0
mgasft
Asked:
mgasft
  • 17
  • 4
1 Solution
 
Bob LearnedCommented:
Mike,

1) You can load a user control from another user control

2) It would probably help to know what kind of UI that you are trying to achieve.

3) There might be some master page magic that would alleviate that requirement of user controls.

Bob
0
 
mgasftAuthor Commented:
Hi Bob

Please have a look athe these screenshots:

http://www.algarveweb.com/list.jpg

http://www.algarveweb.com/edit.jpg

I am using master pages right now.

I have 2 points where I wish to improve the application:
1 - get rid of querystrings, meaning that the user can not tamper with them.
2 - alway show default.aspx as current page so the user can not go to different pages.

Thanks, Mike.
0
 
mgasftAuthor Commented:
Some more info:

This is a multi company/multi user application, therefore the need for addionatal security.

I have tried in the meantime a frameset with a centered content page, but of course there the user can always righ click and find out the path and name of the page.

Mike
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Bob LearnedCommented:
Mike,

I like to have the Menu in the master page, and then have the target for the navigation be to a page that uses the same master page, and displays its contents in the ContentPlaceHolder--no frameset, no user controls.

bob
0
 
mgasftAuthor Commented:
Bob

I am not user that I understand.
This the curent page layout: http://www.algarveweb.com/explorer.jpg
As you can see I use master pages already with 3 ContenPlaceHolders: Top, Left and Content.
But when I navigate it always shows the current page name eg. LIST_main_add.aspx, which I try to avoid.

Mike
0
 
mgasftAuthor Commented:
user = sure
0
 
mgasftAuthor Commented:
How can I load a user control from inside a user control?
0
 
Bob LearnedCommented:
That is an interesting requirement, and one which I don't have a ready answer for.

Bob
0
 
mgasftAuthor Commented:
I got this working from inside uc1.ascx loaded in a Placeholder:

Dim my_plc As New PlaceHolder
my_plc = Page.FindControl("plc")
'my_plc.Controls.Clear() GIVES ERROR
Dim my_control As Control = Page.LoadControl("uc2.ascx")
my_plc.Controls.Add(my_control)

When I try to clear the Placeholder I get:

Object reference not set to an instance of an object.
Dim my_control As Control = Page.LoadControl("uc2.ascx")

Without the Clear id adds to the current content in the Placeholder, whci of course is wrong

I think we are very close now, any ideas?

Mike

0
 
mgasftAuthor Commented:
I CAN CLEAR THE PACEHOLDER

Dim my_plc As New PlaceHolder
my_plc = Page.FindControl("plc")
my_plc.Controls.Clear()

OR ADD TO THE PLACHOLDER

Dim my_plc As New PlaceHolder
my_plc = Page.FindControl("plc")
Dim my_control As Control = Page.LoadControl("uc2.ascx")
my_plc.Controls.Add(my_control)

BUT NOT BOTH

Mike
0
 
mgasftAuthor Commented:
Strange, it works id used directly in aspx mas not in ascx
0
 
Bob LearnedCommented:
1) Where is the PlaceHolder defined?  On the master page?

2) Where are you trying to call this from?

Bob
0
 
mgasftAuthor Commented:
1) No master page, just simple aspx.
2) Inside the first ascx.
0
 
mgasftAuthor Commented:
<%@ Page Language="VB" AutoEventWireup="false" Debug="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Src="uc1.ascx" TagName="uc1" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Button ID="Button1" runat="server" Text="Remove aspx" /><br />
        <br />
        <asp:PlaceHolder ID="plc" runat="server"></asp:PlaceHolder>
   
    </div>
    </form>
</body>
</html>
0
 
mgasftAuthor Commented:
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim my_control As Control = Page.LoadControl("uc1.ascx")

        plc.Controls.Add(my_control)

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim my_control As Control = Page.LoadControl("uc1.ascx")

        plc.Controls.Add(my_control)

        plc.Controls.Clear()

        Dim my_control2 As Control = Page.LoadControl("uc2.ascx")

        plc.Controls.Add(my_control2)
    End Sub
End Class
0
 
mgasftAuthor Commented:
<%@ Control Language="VB" AutoEventWireup="false" Debug="true" CodeFile="uc1.ascx.vb" Inherits="uc1" %>

uc1<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Remove ascx" />
0
 
mgasftAuthor Commented:
Partial Class uc1
    Inherits System.Web.UI.UserControl

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim my_plc As New PlaceHolder

        my_plc = Page.FindControl("plc")

        my_plc.Controls.Clear() ' GIVES ERROR

        Dim my_control As Control = Page.LoadControl("uc2.ascx")

        my_plc.Controls.Add(my_control)

    End Sub
End Class
0
 
mgasftAuthor Commented:
the 2nd ascx is just empty-
0
 
mgasftAuthor Commented:
The importna code is in Partial Class uc1
0
 
mgasftAuthor Commented:
I have uploded my code here
http://www.algarveweb.com/my_user_control.rar
hope that helps to see what i need

Mike
0
 
mgasftAuthor Commented:
Good morning

I god this working inside the 1st ascx:

Dim my_plc As New PlaceHolder
my_plc = Page.FindControl("plc")
Dim my_control As Control = Page.LoadControl("uc2.ascx")
my_plc.Controls.Add(my_control)
my_plc.Controls.RemoveAt(0)

If I remove the control 1st and then add it dows not work!
When I add 1st and the remove it works!

Very strange!!

Mike


0
 
Computer101Commented:
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 17
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now