473,385 Members | 1,742 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,385 software developers and data experts.

How do you use a user control as a template inside a repeater?

I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert

Jul 6 '06 #1
8 2994
First add the user control to the page declaration

<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>

then put the user control in the repeater

<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate>
</asp:Repeater>

then in the codebehind

Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

Dim usc As your_control = e.Item.FindControl("uscControl")

'now you can access all of the public properties and methods of the usercontrol, for example

usc.LabelSet("test",true,"test1")

'and so on

End If

End Sub
--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com

"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.googlegr oups.com...
>I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
Jul 6 '06 #2
David,

Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27

Below is the code I'm using to call the labelSet method:

protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}

Do you know what I'm missing to get this to work?

Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
First add the user control to the page declaration

<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>

then put the user control in the repeater

<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate>
</asp:Repeater>

then in the codebehind

Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

Dim usc As your_control = e.Item.FindControl("uscControl")

'now you can access all of the public properties and methods of the usercontrol, for example

usc.LabelSet("test",true,"test1")

'and so on

End If

End Sub
--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com

"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.googlegr oups.com...
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New" color=#000080 size=2>&lt;</FONT><A
href='mailto:%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%'><FONT
face="Courier New" color=#000080 size=2>%@Register Tagname="usercontrol"
Tagprefix="usa" src="~/controls/usercontrol.ascx"%</FONT></A><FONT
face="Courier New" color=#000080 size=2>&gt;</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then put the user control in
the repeater</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><asp:Repeater id=Repeater1 OnItemDataBound="processList"
runat="server"><ITEMTEMPLATE><?xml:namespace prefix = usa /><usa:usercontrol
id=uscControl runat="Server"><FONT face="Courier New" color=#000080
size=2>&nbsp;&nbsp;&nbsp;&lt;asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ItemTemplate& gt;<STRONG>&lt;usa:usercontrol
runat="Server" id="uscControl"
/&gt;</STRONG>&lt;/ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:Repeater&gt;</FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then in the
codebehind</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>&nbsp;&nbsp;&nbsp;
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; 'now you can access all of the public properties and methods
of the usercontrol, for example</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>&nbsp;&nbsp;&nbsp;
End</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>If</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>End</FONT><FONT
size=2</FONT><FONT color=#0000ff size=2>Sub</P></FONT></FONT></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT><FONT
face="Trebuchet MS" color=#000080 size=2></FONT><BR><FONT face="Trebuchet MS"
color=#000080 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:dlozzi@(remove)delphi-ts.com"><FONT face="Trebuchet MS"
color=#000080 size=2>dlozzi@(remove)delphi-ts.com</FONT></A><BR><A
href="http://www.delphi-ts.com"><FONT face="Trebuchet MS" color=#000080
size=2>www.delphi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr" &lt;</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS" color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>&gt; wrote in message </FONT><A
href="news:11**********************@s26g2000cwa.go oglegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa.g ooglegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>&gt;I would like to use a user control
as a template inside a repeater.<BR>&gt; Some of the fields in the control
should be hidden depending on whether<BR>&gt; or not there is data.&nbsp; I'm
still a ASP .Net newbie so the way I'm going<BR>&gt; about doing this might be a
little off.&nbsp; I'd appreciate some help.<BR>&gt; <BR>&gt; Below is the code I
have thus far but I'm not sure how to reference the<BR>&gt; user control within
the foreach loop.<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Panel ID="pnlRosterProfile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Repeater ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;ItemTemplate&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;/ItemTemplate&gt;<BR>&gt;&nbsp; &lt;/asp:Repeater&gt;<BR>&gt; <BR>&gt; Code
behind:<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>&gt;
strRosterType);<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp; DataRow[]
foundRows =
DS.Tables[0].Select();<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp; foreach
(DataRow dr in foundRows)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; [User control
here?]<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; }<BR>&gt; <BR>&gt;
<BR>&gt; <BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&gt; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireup="true"<BR>&gt; CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Panel ID="pnlProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:Table ID="tblProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;
&lt;asp:Image ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;
&lt;asp:Label ID="lblNameDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmailDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployerDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployer" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitleDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitle" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:Table&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;/asp:Panel&gt;<BR>&gt; <BR>&gt; <BR>&gt; user control code behind:<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp; public void labelSet(string controlName, bool
blnVisible, string<BR>&gt; lblTitle)<BR>&gt;&nbsp;&nbsp;&nbsp;
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; Label
objLabel;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp; objLabel =
(Label)this.FindControl(controlName);<BR>&gt;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if
(blnVisible)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Text = lblTitle;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;
else<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Visible = false;&nbsp;&nbsp;&nbsp; <BR>&gt;&nbsp;&nbsp;&nbsp; }<BR>&gt;
<BR>&gt; <BR>&gt; Thanks,<BR>&gt; Robert<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_007B_01C6A12A.759D9DC0--
Jul 6 '06 #3
Line 27: objLabel.Text = lblTitle;

I believe should be
Line 27: objLabel.Text = lblTitle.text;


--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
David,

Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27

Below is the code I'm using to call the labelSet method:

protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}

Do you know what I'm missing to get this to work?

Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
>First add the user control to the page declaration

<%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%>

then put the user control in the repeater

<asp:Repeater id="Repeater1" runat="server"
OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl"
/></ItemTemplate>
</asp:Repeater>

then in the codebehind

Private Sub processList(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs )

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim usc As your_control = e.Item.FindControl("uscControl")

'now you can access all of the public properties and methods of
the usercontrol, for example

usc.LabelSet("test",true,"test1")

'and so on

End If

End Sub
--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com

"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@s26g2000cwa.googleg roups.com...
>I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user
control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New" color=#000080 size=2>&lt;</FONT><A
href='mailto:%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%'><FONT
face="Courier New" color=#000080 size=2>%@Register Tagname="usercontrol"
Tagprefix="usa" src="~/controls/usercontrol.ascx"%</FONT></A><FONT
face="Courier New" color=#000080 size=2>&gt;</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then put the user
control in
the repeater</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><asp:Repeater id=Repeater1 OnItemDataBound="processList"
runat="server"><ITEMTEMPLATE><?xml:namespace prefix = usa
/><usa:usercontrol
id=uscControl runat="Server"><FONT face="Courier New" color=#000080
size=2>&nbsp;&nbsp;&nbsp;&lt;asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ItemTemplate& gt;<STRONG>&lt;usa:usercontrol
runat="Server" id="uscControl"
/&gt;</STRONG>&lt;/ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:Repeater&gt;</FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then in the
codebehind</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs )</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; 'now you can access all of the public properties and
methods
of the usercontrol, for example</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
End</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>If</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>End</FONT><FONT
size=2</FONT><FONT color=#0000ff
size=2>Sub</P></FONT></FONT></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT><FONT
face="Trebuchet MS" color=#000080 size=2></FONT><BR><FONT face="Trebuchet
MS"
color=#000080 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:dlozzi@(remove)delphi-ts.com"><FONT face="Trebuchet MS"
color=#000080 size=2>dlozzi@(remove)delphi-ts.com</FONT></A><BR><A
href="http://www.delphi-ts.com"><FONT face="Trebuchet MS" color=#000080
size=2>www.delphi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr"
&lt;</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS"
color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>&gt; wrote in message </FONT><A
href="news:11**********************@s26g2000cwa.g ooglegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa. googlegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&gt; Some of the fields in the
control
should be hidden depending on whether<BR>&gt; or not there is data.&nbsp;
I'm
still a ASP .Net newbie so the way I'm going<BR>&gt; about doing this
might be a
little off.&nbsp; I'd appreciate some help.<BR>&gt; <BR>&gt; Below is the
code I
have thus far but I'm not sure how to reference the<BR>&gt; user control
within
the foreach loop.<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Panel
ID="pnlRosterProfile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Repeater
ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;ItemTemplate&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;/ItemTemplate&gt;<BR>&gt;&nbsp; &lt;/asp:Repeater&gt;<BR>&gt;
<BR>&gt; Code
behind:<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>&gt;
strRosterType);<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; [User
control
here?]<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&gt; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireup="true"<BR>&gt; CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Panel
ID="pnlProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:Table ID="tblProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;
&lt;asp:Image ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;
&lt;asp:Label ID="lblNameDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmailDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployerDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployer" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitleDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitle" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:Table&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;/asp:Panel&gt;<BR>&gt; <BR>&gt; <BR>&gt; user control code
behind:<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp; public void labelSet(string controlName, bool
blnVisible, string<BR>&gt; lblTitle)<BR>&gt;&nbsp;&nbsp;&nbsp;
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; Label
objLabel;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp; objLabel =
(Label)this.FindControl(controlName);<BR>&gt;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if
(blnVisible)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Text =
lblTitle;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
else<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Visible = false;&nbsp;&nbsp;&nbsp;
<BR>&gt;&nbsp;&nbsp;&nbsp; }<BR>&gt;
<BR>&gt; <BR>&gt; Thanks,<BR>&gt; Robert<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_007B_01C6A12A.759D9DC0--

Jul 7 '06 #4
My apologies, that is wrong. I misread your code. I thought lblTitle was a
Label, but its a string variable. Anyway, my thoughts on the error is that
this line
>Line 25: objLabel = (Label)this.FindControl(controlName);
is the problem. I'm not familiar with C# at all, i'm a VB developer ;). Is
"this" a valid reference in C#? it isnt in VB, VB uses "me". I don't know
where to go from here without viewing all of the actual code.

good luck

--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com
"David Lozzi" <dl****@nospam.nospamwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
>Line 27: objLabel.Text = lblTitle;

I believe should be
>Line 27: objLabel.Text = lblTitle.text;

--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
>David,

Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27

Below is the code I'm using to call the labelSet method:

protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucR oster");
usc.labelSet("test", true, "test1");
}
}

Do you know what I'm missing to get this to work?

Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
>>First add the user control to the page declaration

<%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%>

then put the user control in the repeater

<asp:Repeater id="Repeater1" runat="server"
OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl"
/></ItemTemplate>
</asp:Repeater>

then in the codebehind

Private Sub processList(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArg s)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim usc As your_control = e.Item.FindControl("uscControl")

'now you can access all of the public properties and methods of
the usercontrol, for example

usc.LabelSet("test",true,"test1")

'and so on

End If

End Sub
--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com

"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@s26g2000cwa.google groups.com...
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on
whether
or not there is data. I'm still a ASP .Net newbie so the way I'm
going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference
the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert

------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user
control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New" color=#000080 size=2>&lt;</FONT><A
href='mailto:%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%'><FONT
face="Courier New" color=#000080 size=2>%@Register Tagname="usercontrol"
Tagprefix="usa" src="~/controls/usercontrol.ascx"%</FONT></A><FONT
face="Courier New" color=#000080 size=2>&gt;</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then put the user
control in
the repeater</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><asp:Repeater id=Repeater1 OnItemDataBound="processList"
runat="server"><ITEMTEMPLATE><?xml:namespace prefix = usa
/><usa:usercontrol
id=uscControl runat="Server"><FONT face="Courier New" color=#000080
size=2>&nbsp;&nbsp;&nbsp;&lt;asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ItemTemplate& gt;<STRONG>&lt;usa:usercontrol
runat="Server" id="uscControl"
/&gt;</STRONG>&lt;/ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:Repeater&gt;</FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then in the
codebehind</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs) </FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; Dim</FONT><FONT size=2usc </FONT><FONT
color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; 'now you can access all of the public properties and
methods
of the usercontrol, for example</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 'and so
on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
End</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>If</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>End</FONT><FONT
size=2</FONT><FONT color=#0000ff
size=2>Sub</P></FONT></FONT></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT><FONT
face="Trebuchet MS" color=#000080 size=2></FONT><BR><FONT
face="Trebuchet MS"
color=#000080 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:dlozzi@(remove)delphi-ts.com"><FONT face="Trebuchet MS"
color=#000080 size=2>dlozzi@(remove)delphi-ts.com</FONT></A><BR><A
href="http://www.delphi-ts.com"><FONT face="Trebuchet MS" color=#000080
size=2>www.delphi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr"
&lt;</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS"
color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>&gt; wrote in message </FONT><A
href="news:11**********************@s26g2000cwa. googlegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa .googlegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&gt; Some of the fields in the
control
should be hidden depending on whether<BR>&gt; or not there is
data.&nbsp; I'm
still a ASP .Net newbie so the way I'm going<BR>&gt; about doing this
might be a
little off.&nbsp; I'd appreciate some help.<BR>&gt; <BR>&gt; Below is
the code I
have thus far but I'm not sure how to reference the<BR>&gt; user control
within
the foreach loop.<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Panel
ID="pnlRosterProfile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Repeater
ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;ItemTemplate&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;/ItemTemplate&gt;<BR>&gt;&nbsp; &lt;/asp:Repeater&gt;<BR>&gt;
<BR>&gt; Code
behind:<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>&gt;
strRosterType);<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; [User
control
here?]<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&gt; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireup="true"<BR>&gt; CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Panel
ID="pnlProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:Table ID="tblProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;
&lt;asp:Image ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;
&lt;asp:Label ID="lblNameDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmailDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployerDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployer" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitleDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitle" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:Table&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;/asp:Panel&gt;<BR>&gt; <BR>&gt; <BR>&gt; user control code
behind:<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp; public void labelSet(string controlName, bool
blnVisible, string<BR>&gt; lblTitle)<BR>&gt;&nbsp;&nbsp;&nbsp;
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp; Label
objLabel;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp; objLabel =
(Label)this.FindControl(controlName);<BR>&gt;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if
(blnVisible)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Text =
lblTitle;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;
else<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Visible = false;&nbsp;&nbsp;&nbsp;
<BR>&gt;&nbsp;&nbsp;&nbsp; }<BR>&gt;
<BR>&gt; <BR>&gt; Thanks,<BR>&gt; Robert<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_007B_01C6A12A.759D9DC0--


Jul 7 '06 #5
After further review of your code, I think you're doing too much work. What
are you trying to accomplish?

--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
David,

Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27

Below is the code I'm using to call the labelSet method:

protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}

Do you know what I'm missing to get this to work?

Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
>First add the user control to the page declaration

<%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%>

then put the user control in the repeater

<asp:Repeater id="Repeater1" runat="server"
OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl"
/></ItemTemplate>
</asp:Repeater>

then in the codebehind

Private Sub processList(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs )

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim usc As your_control = e.Item.FindControl("uscControl")

'now you can access all of the public properties and methods of
the usercontrol, for example

usc.LabelSet("test",true,"test1")

'and so on

End If

End Sub
--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com

"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@s26g2000cwa.googleg roups.com...
>I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user
control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New" color=#000080 size=2>&lt;</FONT><A
href='mailto:%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%'><FONT
face="Courier New" color=#000080 size=2>%@Register Tagname="usercontrol"
Tagprefix="usa" src="~/controls/usercontrol.ascx"%</FONT></A><FONT
face="Courier New" color=#000080 size=2>&gt;</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then put the user
control in
the repeater</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><asp:Repeater id=Repeater1 OnItemDataBound="processList"
runat="server"><ITEMTEMPLATE><?xml:namespace prefix = usa
/><usa:usercontrol
id=uscControl runat="Server"><FONT face="Courier New" color=#000080
size=2>&nbsp;&nbsp;&nbsp;&lt;asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ItemTemplate& gt;<STRONG>&lt;usa:usercontrol
runat="Server" id="uscControl"
/&gt;</STRONG>&lt;/ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:Repeater&gt;</FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then in the
codebehind</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs )</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; 'now you can access all of the public properties and
methods
of the usercontrol, for example</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
End</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>If</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>End</FONT><FONT
size=2</FONT><FONT color=#0000ff
size=2>Sub</P></FONT></FONT></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT><FONT
face="Trebuchet MS" color=#000080 size=2></FONT><BR><FONT face="Trebuchet
MS"
color=#000080 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:dlozzi@(remove)delphi-ts.com"><FONT face="Trebuchet MS"
color=#000080 size=2>dlozzi@(remove)delphi-ts.com</FONT></A><BR><A
href="http://www.delphi-ts.com"><FONT face="Trebuchet MS" color=#000080
size=2>www.delphi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr"
&lt;</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS"
color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>&gt; wrote in message </FONT><A
href="news:11**********************@s26g2000cwa.g ooglegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa. googlegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&gt; Some of the fields in the
control
should be hidden depending on whether<BR>&gt; or not there is data.&nbsp;
I'm
still a ASP .Net newbie so the way I'm going<BR>&gt; about doing this
might be a
little off.&nbsp; I'd appreciate some help.<BR>&gt; <BR>&gt; Below is the
code I
have thus far but I'm not sure how to reference the<BR>&gt; user control
within
the foreach loop.<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Panel
ID="pnlRosterProfile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Repeater
ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;ItemTemplate&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;/ItemTemplate&gt;<BR>&gt;&nbsp; &lt;/asp:Repeater&gt;<BR>&gt;
<BR>&gt; Code
behind:<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>&gt;
strRosterType);<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; [User
control
here?]<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&gt; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireup="true"<BR>&gt; CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Panel
ID="pnlProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:Table ID="tblProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;
&lt;asp:Image ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;
&lt;asp:Label ID="lblNameDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmailDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployerDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployer" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitleDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitle" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:Table&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;/asp:Panel&gt;<BR>&gt; <BR>&gt; <BR>&gt; user control code
behind:<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp; public void labelSet(string controlName, bool
blnVisible, string<BR>&gt; lblTitle)<BR>&gt;&nbsp;&nbsp;&nbsp;
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; Label
objLabel;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp; objLabel =
(Label)this.FindControl(controlName);<BR>&gt;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if
(blnVisible)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Text =
lblTitle;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
else<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Visible = false;&nbsp;&nbsp;&nbsp;
<BR>&gt;&nbsp;&nbsp;&nbsp; }<BR>&gt;
<BR>&gt; <BR>&gt; Thanks,<BR>&gt; Robert<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_007B_01C6A12A.759D9DC0--

Jul 7 '06 #6
There are many possible solutions, of which I favor the following:
Create a class which implements ITemplate.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace localhost
{
public class RepeaterTest : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected MyRepeater myRepeater;

private void Page_Load(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col", typeof(string));
dt.Rows.Add(new object[] {"One"});
dt.Rows.Add(new object[] {"Two"});
myRepeater.DataSource = dt.DefaultView;
myRepeater.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

myRepeater = new MyRepeater();
Form1.Controls.Add(myRepeater);
}
#endregion
}

// One way to specify your template is to inherit from Repeater, but
you can also set the Template as a property before DataBind()
public class MyRepeater : Repeater
{
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
base.HeaderTemplate = new LiteralTemplate("<table>"); // Custom
Header
base.ItemTemplate = new MyTemplate(); // Custom Item Template
base.FooterTemplate = new LiteralTemplate("</table>"); // Custom
Footer
}
}

public class MyTemplate : ITemplate
{
#region ITemplate Members

public void InstantiateIn(Control container)
{
PlaceHolder Me = new PlaceHolder(); // functions as a container for
your controls
container.Controls.Add(Me); // Add to Controls collection
Me.DataBinding += new EventHandler(Me_DataBinding); // Bind to
DataBinding event
}

#endregion

// For each iteration of data do the following
private void Me_DataBinding(object sender, EventArgs e)
{
PlaceHolder Me = (PlaceHolder) sender; // Cast back to container
control
RepeaterItem container = (RepeaterItem) Me.NamingContainer; //
Casst to Item instance
DataRowView drv = (DataRowView) container.DataItem; // -or-
e.Item.DataItem;
//Repeater repeater = (Repeater) Me.NamingContainer.NamingContainer;
// Optionally you can access the parent repeater.
//DataView datasource = repeater.DataSource as DataView; //
Optionally you can access the original datasource.

Me.Controls.Add(new
LiteralControl(String.Format("<tr><td>{0}</td></tr>", drv[0]))); //
Add your control / content.
}
}
}

fernandezr wrote:
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
Jul 7 '06 #7
David,

After trying to implement your code I simplified mine. Hopefully this
will clear things up about what I'm trying to do. I have a user
control that will display for each record returned by the dataset. If
fields are blank I will hide those rows in the template for that
record. I am planning on reusing this control on several pages.

<%@ Register TagPrefix="uc1" TagName="profile"
src="~/controls/ucRosterProfile.ascx" %>

<asp:Repeater ID="rptRoster" runat="Server"
OnItemDataBound="processList" >
<ItemTemplate>
<uc1:profile runat="Server" id="ucRoster" />
</ItemTemplate>
</asp:Repeater>

-------------
code behind

DataSet DS = SQLRoutines.GetRosters(int.Parse(strUID), strCCYYS,
int.Parse(strRosterType));
rptRoster.DataSource = DS;
rptRoster.DataBind();

protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}

--------------------------------------
user control:
<asp:Panel ID="pnlRosterProfile" runat="Server">
<asp:Table ID="tblRosterProfile" runat="Server">
<asp:TableRow>
<asp:TableCell Width="30">
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell Width="45">

<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>

-----
code behind

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}

Thanks,
Happy Friday,
Robert
David Lozzi wrote:
After further review of your code, I think you're doing too much work. What
are you trying to accomplish?

--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
David,

Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27

Below is the code I'm using to call the labelSet method:

protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}

Do you know what I'm missing to get this to work?

Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
First add the user control to the page declaration

<%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%>

then put the user control in the repeater

<asp:Repeater id="Repeater1" runat="server"
OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl"
/></ItemTemplate>
</asp:Repeater>

then in the codebehind

Private Sub processList(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim usc As your_control = e.Item.FindControl("uscControl")

'now you can access all of the public properties and methods of
the usercontrol, for example

usc.LabelSet("test",true,"test1")

'and so on

End If

End Sub
--
David Lozzi
dlozzi@(remove)delphi-ts.com
www.delphi-ts.com

"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@s26g2000cwa.googlegr oups.com...
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert

------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user
control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New" color=#000080 size=2>&lt;</FONT><A
href='mailto:%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%'><FONT
face="Courier New" color=#000080 size=2>%@Register Tagname="usercontrol"
Tagprefix="usa" src="~/controls/usercontrol.ascx"%</FONT></A><FONT
face="Courier New" color=#000080 size=2>&gt;</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then put the user
control in
the repeater</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><asp:Repeater id=Repeater1 OnItemDataBound="processList"
runat="server"><ITEMTEMPLATE><?xml:namespace prefix = usa
/><usa:usercontrol
id=uscControl runat="Server"><FONT face="Courier New" color=#000080
size=2>&nbsp;&nbsp;&nbsp;&lt;asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ItemTemplate& gt;<STRONG>&lt;usa:usercontrol
runat="Server" id="uscControl"
/&gt;</STRONG>&lt;/ItemTemplate&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/asp:Repeater&gt;</FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>then in the
codebehind</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; 'now you can access all of the public properties and
methods
of the usercontrol, for example</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&nbsp;&nbsp;
End</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>If</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>End</FONT><FONT
size=2</FONT><FONT color=#0000ff
size=2>Sub</P></FONT></FONT></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT><FONT
face="Trebuchet MS" color=#000080 size=2></FONT><BR><FONT face="Trebuchet
MS"
color=#000080 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:dlozzi@(remove)delphi-ts.com"><FONT face="Trebuchet MS"
color=#000080 size=2>dlozzi@(remove)delphi-ts.com</FONT></A><BR><A
href="http://www.delphi-ts.com"><FONT face="Trebuchet MS" color=#000080
size=2>www.delphi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr"
&lt;</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS"
color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>&gt; wrote in message </FONT><A
href="news:11**********************@s26g2000cwa.go oglegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa.g ooglegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&gt; Some of the fields in the
control
should be hidden depending on whether<BR>&gt; or not there is data.&nbsp;
I'm
still a ASP .Net newbie so the way I'm going<BR>&gt; about doing this
might be a
little off.&nbsp; I'd appreciate some help.<BR>&gt; <BR>&gt; Below is the
code I
have thus far but I'm not sure how to reference the<BR>&gt; user control
within
the foreach loop.<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Panel
ID="pnlRosterProfile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp; &lt;asp:Repeater
ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;ItemTemplate&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;
&lt;/ItemTemplate&gt;<BR>&gt;&nbsp; &lt;/asp:Repeater&gt;<BR>&gt;
<BR>&gt; Code
behind:<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>&gt;
strRosterType);<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; [User
control
here?]<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&gt; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireup="true"<BR>&gt; CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Panel
ID="pnlProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:Table ID="tblProfile"
runat="Server"&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;
&lt;asp:Image ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;
&lt;asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;
&lt;asp:Label ID="lblNameDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmailDesc" runat="Server"<BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployerDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblEmployer" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitleDesc" runat="Server"<BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;asp:Label ID="lblJobTitle" runat="Server" Text=""
/&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableCell&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:TableRow&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/asp:Table&gt;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;/asp:Panel&gt;<BR>&gt; <BR>&gt; <BR>&gt; user control code
behind:<BR>&gt;
<BR>&gt;&nbsp;&nbsp;&nbsp; public void labelSet(string controlName, bool
blnVisible, string<BR>&gt; lblTitle)<BR>&gt;&nbsp;&nbsp;&nbsp;
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; Label
objLabel;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp; objLabel =
(Label)this.FindControl(controlName);<BR>&gt;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if
(blnVisible)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Text =
lblTitle;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;
else<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;
objLabel.Visible = false;&nbsp;&nbsp;&nbsp;
<BR>&gt;&nbsp;&nbsp;&nbsp; }<BR>&gt;
<BR>&gt; <BR>&gt; Thanks,<BR>&gt; Robert<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_007B_01C6A12A.759D9DC0--
Jul 7 '06 #8
Could you tell me why you favor this solution over the others? I have
seen a few articles where people mention different solutions, like you
say, it is hard to figure out which would be the best road to take. I
would like the solution to be reusable and efficient.

Thanks,
Robert
ka*******@gmail.com wrote:
There are many possible solutions, of which I favor the following:
Create a class which implements ITemplate.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace localhost
{
public class RepeaterTest : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected MyRepeater myRepeater;

private void Page_Load(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col", typeof(string));
dt.Rows.Add(new object[] {"One"});
dt.Rows.Add(new object[] {"Two"});
myRepeater.DataSource = dt.DefaultView;
myRepeater.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

myRepeater = new MyRepeater();
Form1.Controls.Add(myRepeater);
}
#endregion
}

// One way to specify your template is to inherit from Repeater, but
you can also set the Template as a property before DataBind()
public class MyRepeater : Repeater
{
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
base.HeaderTemplate = new LiteralTemplate("<table>"); // Custom
Header
base.ItemTemplate = new MyTemplate(); // Custom Item Template
base.FooterTemplate = new LiteralTemplate("</table>"); // Custom
Footer
}
}

public class MyTemplate : ITemplate
{
#region ITemplate Members

public void InstantiateIn(Control container)
{
PlaceHolder Me = new PlaceHolder(); // functions as a container for
your controls
container.Controls.Add(Me); // Add to Controls collection
Me.DataBinding += new EventHandler(Me_DataBinding); // Bind to
DataBinding event
}

#endregion

// For each iteration of data do the following
private void Me_DataBinding(object sender, EventArgs e)
{
PlaceHolder Me = (PlaceHolder) sender; // Cast back to container
control
RepeaterItem container = (RepeaterItem) Me.NamingContainer; //
Casst to Item instance
DataRowView drv = (DataRowView) container.DataItem; // -or-
e.Item.DataItem;
//Repeater repeater = (Repeater) Me.NamingContainer.NamingContainer;
// Optionally you can access the parent repeater.
//DataView datasource = repeater.DataSource as DataView; //
Optionally you can access the original datasource.

Me.Controls.Add(new
LiteralControl(String.Format("<tr><td>{0}</td></tr>", drv[0]))); //
Add your control / content.
}
}
}

fernandezr wrote:
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.

<asp:Panel ID="pnlRosterProfile" runat="Server" />

<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>

Code behind:

DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{

[User control here?]
}

-------------------------------------------------
User control:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>

<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:

public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
Jul 7 '06 #9

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

Similar topics

0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
4
by: huzz | last post by:
I am trying to access a DropDownList control inside a repeater using ItemCommand as shown below but for some reason i can't access the DropDownList. When i step through the debug i get <undefine...
2
by: Andy Fish | last post by:
Hi, First some background: When you databind a repeater control, the controls within the template are given an id like Repeater1:_ctl<n>:Button1 where <n> increments for each repeater item. If...
0
by: mike | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
0
by: news_server.nc.rr.com | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
1
by: Jonathan Wood | last post by:
Okay, as evidenced by other questions, I am an experienced programmer very new to ASP.NET. I have a vertical navigation bar. It is all one color with several panels inside it with a different...
1
by: Dave | last post by:
I have the following ASP.NET 2.0 code (simplified here for ease): <asp:Repeater id="SearchResultsRepeater" runat="server"> <ItemTemplate> <uc:SearchResult ID="SearchResult"...
7
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm...
3
by: Emma Middlebrook | last post by:
Hi there, I've been trying to implement a repeater control in an ASP.NET 2 page but I can't seem to get the layout exactly how I want and I'm not sure if it's something that I am doing wrong or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.