Link to home
Start Free TrialLog in
Avatar of mgasft
mgasft

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of mgasft
mgasft

ASKER

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.
Avatar of mgasft

ASKER

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
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
Avatar of mgasft

ASKER

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
Avatar of mgasft

ASKER

user = sure
Avatar of mgasft

ASKER

How can I load a user control from inside a user control?
That is an interesting requirement, and one which I don't have a ready answer for.

Bob
Avatar of mgasft

ASKER

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

Avatar of mgasft

ASKER

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
Avatar of mgasft

ASKER

Strange, it works id used directly in aspx mas not in ascx
1) Where is the PlaceHolder defined?  On the master page?

2) Where are you trying to call this from?

Bob
Avatar of mgasft

ASKER

1) No master page, just simple aspx.
2) Inside the first ascx.
Avatar of mgasft

ASKER

<%@ 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>
Avatar of mgasft

ASKER

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
Avatar of mgasft

ASKER

<%@ 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" />
Avatar of mgasft

ASKER

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
Avatar of mgasft

ASKER

the 2nd ascx is just empty-
Avatar of mgasft

ASKER

The importna code is in Partial Class uc1
Avatar of mgasft

ASKER

I have uploded my code here
http://www.algarveweb.com/my_user_control.rar
hope that helps to see what i need

Mike
Avatar of mgasft

ASKER

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


ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial