Master pages and user controls

S

Scott

I'm using a master page (mp_TableMaint.aspx) with one Content area (ID:
Content1). Content1 contains several usercontrols, which are hidden. The
goal is to show the correct control based on the querystring passed when
calling mp_TableMaint (the page is called from an ASP.NET 2.0 Menu control
on my index.aspx page). I believe I understand correctly that the Master
Page's ASP Content control (named Content1) is, itself, a server side
control. My problem is determining how to show/hide my other user controls
based on user selection. I'm using VB, and have tried every one of the page
events to determine when the controls are populated into Content1, but
obviously I'm doing something wrong.

I've tried this:

Me.Controls(0).ID

which results in a control named "ct001" or something

Also tried:

Me.Controls(0).Controls.Count

This gives me a count of 5, which is correct, but if I do this:

Me.Controls(0).Controls(1).ID (or any other number instead of 1)

in the Immediate window VS returns "Nothing"

I've also tried using FindControl to locate the control.

Is this the best way to achieve this? All I need to do is show/hide a
usercontrol on my Content page based on the querystring value passed in.

I'm using Visual Studio 2005 if it makes a difference.

I don't know if this helps, but here's the source from the table:


<%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false"

CodeFile="mp_TableMaint.aspx.vb" Inherits="webPilotLogbook.mp_TableMaint"
Title="Tables" %>

<%@ Register Src="WebControls/ratings.ascx" TagName="ratings"
TagPrefix="uc8" %>

<%@ Register Src="WebControls/powercharacteristics.ascx"
TagName="powercharacteristics"

TagPrefix="uc7" %>

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

<%@ Register Src="WebControls/aircraftclass.ascx" TagName="aircraftclass"
TagPrefix="uc2" %>

<%@ Register Src="WebControls/aircraftclassification.ascx"
TagName="aircraftclassification"

TagPrefix="uc3" %>

<%@ Register Src="WebControls/aircraftid2.ascx" TagName="aircraftid2"
TagPrefix="uc4" %>

<%@ Register Src="WebControls/make.ascx" TagName="make" TagPrefix="uc5" %>

<%@ Register Src="WebControls/certificates.ascx" TagName="certificates"
TagPrefix="uc6" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">

<table width="100%" cellpadding="0" cellspacing="0" border="0">

<tr>

<td>

</td>

<td>

<table align="center">

<tr>

<td align="center">

<%-- <uc1:AircraftCategory id="AircraftCategory1" runat="server"
Visible="False" />

<uc1:AircraftClass id="AircraftClass1" runat="server"
Visible="False"></uc1:AircraftClass>

<uc1:AircraftClassification id="AircraftClassification1" runat="server"
Visible="False"></uc1:AircraftClassification>

<uc1:Certificates id="Certificates1" runat="server"
Visible="False"></uc1:Certificates>

<uc1:Make1 id="Make1" runat="server" Visible="False"></uc1:Make1>

<uc1:powerCharacteristics id="PowerCharacteristics1" runat="server"
Visible="False"></uc1:powerCharacteristics>

<uc1:Ratings id="Ratings1" runat="server" Visible="False"></uc1:Ratings>

<uc1:AircraftId2 id="AircraftId21" runat="server"
Visible="False"></uc1:AircraftId2>

--%>

<uc1:aircraftcategory ID="Aircraftcategory1" runat="server" />

<uc7:powercharacteristics ID="Powercharacteristics1" runat="server" />

<uc8:ratings ID="Ratings1" runat="server" />

<uc6:certificates ID="Certificates1" runat="server" />

<uc5:make ID="Make1" runat="server" />

<uc4:aircraftid2 ID="Aircraftid2_1" runat="server" />

<uc3:aircraftclassification ID="Aircraftclassification1" runat="server" />

<uc2:aircraftclass ID="Aircraftclass1" runat="server" />

</td>

</tr>

</table>

</td>

<td>

</td>

</tr>

</table>

</asp:Content>
 
K

Ken Cox - Microsoft MVP

Hi Scott,

You have to take a long walk down the control tree to get where you want to
go. The trick is to turn trace="true" on your .aspx page so you can see
where the tree goes. However, watch out for the form because I was looking
for "aspnetForm" but it turned out to be "form1".

Try this example and see if it helps you get there? Let us know?

Ken
Microsoft MVP [ASP.NET]


Here's contnt.aspx:

<%@ Page Language="VB" trace="true" MasterPageFile="~/MasterPage.master"
Title="Untitled Page" %>

<%@ register src="usrcntrl1.ascx" tagname="usrcntrl1" tagprefix="uc1" %>
<%@ register src="usrcntrl2.ascx" tagname="usrcntrl2" tagprefix="uc2" %>

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim req As String
req = "1"
If req = "1" Then
Dim frm As HtmlForm
' Get the form
frm = Page.Controls(0).FindControl("form1")
If Not IsNothing(frm) Then
Dim cph As ContentPlaceHolder
' Get the placeholder
cph = frm.FindControl("ContentPlaceHolder1")
If Not IsNothing(cph) Then
Dim usr As UserControl
' Get the user control
usr = cph.FindControl("Usrcntrl1_1")
If Not IsNothing(usr) Then
usr.Visible = True
Dim txtbx As TextBox
txtbx = usr.FindControl("Textbox1")
If Not IsNothing(txtbx) Then
txtbx.Text = "Finally!"
End If
End If
End If
End If
End If
End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<uc1:usrcntrl1 id="Usrcntrl1_1" runat="server" visible="false">
</uc1:usrcntrl1>
<br />
<br />
<uc2:usrcntrl2 id="Usrcntrl2_1" runat="server" visible="false">
</uc2:usrcntrl2>
<br />
<br />
</asp:Content>


Here's usrcntrl1.ascx:

<%@ Control Language="VB" ClassName="usrcntrl1" %>

<script runat="server">

</script>

<asp:label id="Label1" runat="server" text="This is in
Usrcntrl1"></asp:label><br />
<asp:textbox id="TextBox1" runat="server"></asp:textbox>

Here's usrcntrl2.ascx:

<%@ Control Language="VB" ClassName="usrcntrl2" %>

<script runat="server">

</script>

<asp:label id="Label1" runat="server" text="User control 2"></asp:label>
<br />
<asp:imagebutton id="ImageButton1" runat="server" height="101px"
imageurl="http://www.gc.ca/images/rollingflag.jpg"
width="201px" />

Here's MasterPage.master:

<%@ Master Language="VB" %>

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

<script runat="server">



Protected Sub linkbutton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>Home
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
<asp:linkbutton id="linkbutton1" runat="server" text="Navigate"
onclick="linkbutton1_Click" />
</div>
</form>
</body>
</html>
 
S

Scott

Thank you very much Ken. That works perfectly. I'm able to grab the value
passed in from the querystring by using Request.Querystring (is there a
better way of doing this, by the way), walk down to the ContentPlaceHolder
and then look for the specific user control and set values accordingly. And
thanks for the tip on trace="true" ...
--
Scott McDaniel
InfoTrakker Software


Ken Cox - Microsoft MVP said:
Hi Scott,

You have to take a long walk down the control tree to get where you want
to go. The trick is to turn trace="true" on your .aspx page so you can see
where the tree goes. However, watch out for the form because I was
looking for "aspnetForm" but it turned out to be "form1".

Try this example and see if it helps you get there? Let us know?

Ken
Microsoft MVP [ASP.NET]


Here's contnt.aspx:

<%@ Page Language="VB" trace="true" MasterPageFile="~/MasterPage.master"
Title="Untitled Page" %>

<%@ register src="usrcntrl1.ascx" tagname="usrcntrl1" tagprefix="uc1" %>
<%@ register src="usrcntrl2.ascx" tagname="usrcntrl2" tagprefix="uc2" %>

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim req As String
req = "1"
If req = "1" Then
Dim frm As HtmlForm
' Get the form
frm = Page.Controls(0).FindControl("form1")
If Not IsNothing(frm) Then
Dim cph As ContentPlaceHolder
' Get the placeholder
cph = frm.FindControl("ContentPlaceHolder1")
If Not IsNothing(cph) Then
Dim usr As UserControl
' Get the user control
usr = cph.FindControl("Usrcntrl1_1")
If Not IsNothing(usr) Then
usr.Visible = True
Dim txtbx As TextBox
txtbx = usr.FindControl("Textbox1")
If Not IsNothing(txtbx) Then
txtbx.Text = "Finally!"
End If
End If
End If
End If
End If
End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<uc1:usrcntrl1 id="Usrcntrl1_1" runat="server" visible="false">
</uc1:usrcntrl1>
<br />
<br />
<uc2:usrcntrl2 id="Usrcntrl2_1" runat="server" visible="false">
</uc2:usrcntrl2>
<br />
<br />
</asp:Content>


Here's usrcntrl1.ascx:

<%@ Control Language="VB" ClassName="usrcntrl1" %>

<script runat="server">

</script>

<asp:label id="Label1" runat="server" text="This is in
Usrcntrl1"></asp:label><br />
<asp:textbox id="TextBox1" runat="server"></asp:textbox>

Here's usrcntrl2.ascx:

<%@ Control Language="VB" ClassName="usrcntrl2" %>

<script runat="server">

</script>

<asp:label id="Label1" runat="server" text="User control 2"></asp:label>
<br />
<asp:imagebutton id="ImageButton1" runat="server" height="101px"
imageurl="http://www.gc.ca/images/rollingflag.jpg"
width="201px" />

Here's MasterPage.master:

<%@ Master Language="VB" %>

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

<script runat="server">



Protected Sub linkbutton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>Home
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
<asp:linkbutton id="linkbutton1" runat="server" text="Navigate"
onclick="linkbutton1_Click" />
</div>
</form>
</body>
</html>
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top