473,888 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = false;
}
Thanks,
Robert

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

<%@Register Tagname="userco ntrol" Tagprefix="usa" src="~/controls/usercontrol.asc x"%>

then put the user control in the repeater

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

then in the codebehind

Private Sub processList(ByV al sender As Object, ByVal e As System.Web.UI.W ebControls.Repe aterItemEventAr gs)

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType = ListItemType.Al ternatingItem Then

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

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

usc.LabelSet("t est",true,"test 1")

'and so on

End If

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

"fernandezr " <ro************ **@gmail.comwro te in message news:11******** **************@ s26g2000cwa.goo glegroups.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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = 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.NullRefe renceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.Fin dControl(contro lName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visibl e = false;
Source File: d:\acstest\webr oot\MOR\control s\ucRosterProfi le.ascx.cs
Line: 27

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

protected void processList(obj ect sender,
System.Web.UI.W ebControls.Repe aterItemEventAr gs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) || (e.Item.ItemTyp e
== ListItemType.Al ternatingItem))
{
controls_ucRost erProfile usc =
(controls_ucRos terProfile)e.It em.FindControl( "ucRoster") ;
usc.labelSet("t est", 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="userco ntrol" Tagprefix="usa" src="~/controls/usercontrol.asc x"%>

then put the user control in the repeater

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

then in the codebehind

Private Sub processList(ByV al sender As Object, ByVal e As System.Web.UI.W ebControls.Repe aterItemEventAr gs)

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType = ListItemType.Al ternatingItem Then

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

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

usc.LabelSet("t est",true,"test 1")

'and so on

End If

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

"fernandezr " <ro************ **@gmail.comwro te in message news:11******** **************@ s26g2000cwa.goo glegroups.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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = false;
}
Thanks,
Robert
------=_NextPart_000_ 007B_01C6A12A.7 59D9DC0
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="userco ntrol" Tagprefix="usa" src="~/controls/usercontrol.asc x"%'><FONT
face="Courier New" color=#000080 size=2>%@Regist er Tagname="userco ntrol"
Tagprefix="usa" src="~/controls/usercontrol.asc x"%</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:Repea ter id=Repeater1 OnItemDataBound ="processLis t"
runat="server"> <ITEMTEMPLATE>< ?xml:namespace prefix = usa /><usa:usercontr ol
id=uscControl runat="Server"> <FONT face="Courier New" color=#000080
size=2>&nbsp;&n bsp;&nbsp;&lt;a sp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemD ataBound="proce ssList"</STRONG>&gt;<BR> &nbsp;&nbsp;&nb sp;&lt;ItemTemp late&gt;<STRONG >&lt;usa:userco ntrol
runat="Server" id="uscControl "
/&gt;</STRONG>&lt;/ItemTemplate&gt ;<BR>&nbsp;&nbs p;&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=2processLi st(</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.W ebControls.Repe aterItemEventAr gs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>&nbsp;&n bsp;&nbsp;
If</FONT><FONT size=2e.Item.It emType = ListItemType.It em </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.It emType =
ListItemType.Al ternatingItem </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;&n bsp;&nbsp;
&nbsp;&nbsp;&nb sp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_cont rol =
e.Item.FindCont rol("uscControl ")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&n bsp;&nbsp;
&nbsp;&nbsp;&nb sp; '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;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
usc.LabelSet("t est",true,"test 1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>& nbsp;&nbsp;&nbs p;&nbsp;&nbsp; &nbsp; 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>&nbsp;&n bsp;&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:dl ozzi@(remove)de lphi-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.delp hi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernand ezr" &lt;</FONT><A
href="mailto:ro **************@ gmail.com"><FON T 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** *************** *****@s26g2000c wa.googlegroups .com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11* *************** ******@s26g2000 cwa.googlegroup s.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>&g t; 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:Pane l ID="pnlRosterPr ofile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp ; &lt;asp:Repeate r ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp;
&lt;ItemTemplat e&gt;<BR>&gt;&n bsp;&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;&nbs p;&nbsp;&nbsp;& nbsp; DataSet DS =
SQLRoutines.Get Profiles(strUID , strCCYYS,<BR>&g t;
strRosterType); <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; DataRow[]
foundRows =
DS.Tables[0].Select();<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; foreach
(DataRow dr in foundRows)<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; [User control
here?]<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; }<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="ucPro file.ascx.cs"
Inherits="contr ols_ucRosterPro file" %&gt;<BR>&gt;
<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp; &lt;asp:Pane l ID="pnlProfile "
runat="Server"& gt;<BR>&gt;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
&lt;asp:Tabl e ID="tblProfile "
runat="Server"& gt;<BR>&gt;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;
&lt;asp:TableRo w&gt;<BR>&gt;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;asp:TableCe ll&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;asp:Imag e ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
&lt;asp:TableCe ll&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;asp:Labe l ID="lblNameDesc " runat="Server"< BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmailDes c" runat="Server"< BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer Desc" runat="Server"< BR>&gt; Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle Desc" runat="Server"< BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;/asp:TableRow&gt ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;/asp:Table&gt;<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:Panel&gt;<B R>&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>&g t;&nbsp;&nbsp;& nbsp;
{<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp; Label
objLabel;<BR>&g t;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp; objLabel =
(Label)this.Fin dControl(contro lName);<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
if
(blnVisible)<BR >&gt;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;
objLabel.Text = lblTitle;<BR>&g t;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
else<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
objLabel.Visibl e = false;&nbsp;&nb sp;&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.7 59D9DC0--
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.comwro te in message
news:11******** **************@ k73g2000cwa.goo glegroups.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.NullRefe renceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.Fin dControl(contro lName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visibl e = false;
Source File: d:\acstest\webr oot\MOR\control s\ucRosterProfi le.ascx.cs
Line: 27

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

protected void processList(obj ect sender,
System.Web.UI.W ebControls.Repe aterItemEventAr gs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) || (e.Item.ItemTyp e
== ListItemType.Al ternatingItem))
{
controls_ucRost erProfile usc =
(controls_ucRos terProfile)e.It em.FindControl( "ucRoster") ;
usc.labelSet("t est", 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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%>

then put the user control in the repeater

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

then in the codebehind

Private Sub processList(ByV al sender As Object, ByVal e As
System.Web.UI. WebControls.Rep eaterItemEventA rgs)

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.A lternatingItem Then

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

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

usc.LabelSet("t est",true,"test 1")

'and so on

End If

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

"fernandezr " <ro************ **@gmail.comwro te in message
news:11******* *************** @s26g2000cwa.go oglegroups.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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = false;
}
Thanks,
Robert
------=_NextPart_000_ 007B_01C6A12A.7 59D9DC0
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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%'><FONT
face="Courie r New" color=#000080 size=2>%@Regist er Tagname="userco ntrol"
Tagprefix="usa " src="~/controls/usercontrol.asc x"%</FONT></A><FONT
face="Courie r 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:Repe ater id=Repeater1 OnItemDataBound ="processLis t"
runat="server" ><ITEMTEMPLATE> <?xml:namespa ce prefix = usa
/><usa:usercontr ol
id=uscContro l runat="Server"> <FONT face="Courier New" color=#000080
size=2>&nbsp;& nbsp;&nbsp;&lt; asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItem DataBound="proc essList"</STRONG>&gt;<BR> &nbsp;&nbsp;&nb sp;&lt;ItemTemp late&gt;<STRONG >&lt;usa:userco ntrol
runat="Serve r" id="uscControl "
/&gt;</STRONG>&lt;/ItemTemplate&gt ;<BR>&nbsp;&nbs p;&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=2processLi st(</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>Objec t</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.Rep eaterItemEventA rgs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;& nbsp;&nbsp;
If</FONT><FONT size=2e.Item.It emType = ListItemType.It em </FONT><FONT
color=#0000f f size=2>Or</FONT><FONT size=2e.Item.It emType =
ListItemType.A lternatingItem </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;&n bsp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_cont rol =
e.Item.FindCon trol("uscContro l")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&n bsp;&nbsp;
&nbsp;&nbsp;&n bsp; '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;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
usc.LabelSet(" test",true,"tes t1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000> &nbsp;&nbsp;&nb sp;&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="Trebuche t MS" color=#000080 size=2></FONT><BR><FONT face="Trebuchet
MS"
color=#00008 0 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:d lozzi@(remove)d elphi-ts.com"><FONT face="Trebuchet MS"
color=#00008 0 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.del phi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernand ezr"
&lt;</FONT><A
href="mailto:r o************** @gmail.com"><FO NT face="Trebuchet MS"
color=#00008 0
size=2>ro***** *********@gmail .com</FONT></A><FONT face="Trebuchet MS"
color=#00008 0 size=2>&gt; wrote in message </FONT><A
href="news:11* *************** ******@s26g2000 cwa.googlegroup s.com"><FONT
face="Trebuche t MS" color=#000080
size=2>news:11 *************** *******@s26g200 0cwa.googlegrou ps.com</FONT></A><FONT
face="Trebuche t MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuche t MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&g t; 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:Pane l
ID="pnlRosterP rofile"
runat="Serve r" /&gt;<BR>&gt; <BR>&gt;&nbsp ; &lt;asp:Repeate r
ID="rptRoste r"
runat="Serve r" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp;
&lt;ItemTempla te&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;
&lt;/ItemTemplate&gt ;<BR>&gt;&nbsp ; &lt;/asp:Repeater&gt ;<BR>&gt;
<BR>&gt; Code
behind:<BR>&gt ; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
DataSet DS =
SQLRoutines.Ge tProfiles(strUI D, strCCYYS,<BR>&g t;
strRosterType) ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR> &gt;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
{<BR>&gt; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; [User
control
here?]<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&g t; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireu p="true"<BR>&gt ; CodeFile="ucPro file.ascx.cs"
Inherits="cont rols_ucRosterPr ofile" %&gt;<BR>&gt;
<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &lt;asp:Pane l
ID="pnlProfile "
runat="Server" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
&lt;asp:Tabl e ID="tblProfile "
runat="Server" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;asp:TableR ow&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;
&lt;asp:TableC ell&gt;<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;asp:Imag e ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
&lt;asp:TableC ell&gt;<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;asp:Labe l ID="lblNameDesc " runat="Server"< BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmailDes c" runat="Server"< BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Employer "
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;/asp:TableRow&gt ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;/asp:Table&gt;<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:Panel&gt;<B R>&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>&g t;&nbsp;&nbsp;& nbsp;
{<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp; Label
objLabel;<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; objLabel =
(Label)this.Fi ndControl(contr olName);<BR>&gt ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
if
(blnVisible)<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
objLabel.Tex t =
lblTitle;<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
else<BR>&gt;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;
objLabel.Visib le = false;&nbsp;&nb sp;&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.7 59D9DC0--

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.Fin dControl(contro lName);
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******** ******@TK2MSFTN GP03.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.comwro te in message
news:11******** **************@ k73g2000cwa.goo glegroups.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.NullRefe renceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.Fin dControl(contro lName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visibl e = false;
Source File: d:\acstest\webr oot\MOR\control s\ucRosterProfi le.ascx.cs
Line: 27

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

protected void processList(obj ect sender,
System.Web.UI. WebControls.Rep eaterItemEventA rgs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) || (e.Item.ItemTyp e
== ListItemType.Al ternatingItem))
{
controls_ucRost erProfile usc =
(controls_ucRo sterProfile)e.I tem.FindControl ("ucRoster") ;
usc.labelSet("t est", 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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%>

then put the user control in the repeater

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

then in the codebehind

Private Sub processList(ByV al sender As Object, ByVal e As
System.Web.UI .WebControls.Re peaterItemEvent Args)

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

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

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

usc.LabelSet("t est",true,"test 1")

'and so on

End If

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

"fernandezr " <ro************ **@gmail.comwro te in message
news:11****** *************** *@s26g2000cwa.g ooglegroups.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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPr ofile.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = false;
}
Thanks,
Robert

------=_NextPart_000_ 007B_01C6A12A.7 59D9DC0
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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%'><FONT
face="Couri er New" color=#000080 size=2>%@Regist er Tagname="userco ntrol"
Tagprefix="us a" src="~/controls/usercontrol.asc x"%</FONT></A><FONT
face="Couri er 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:Rep eater id=Repeater1 OnItemDataBound ="processLis t"
runat="server "><ITEMTEMPLATE ><?xml:namespac e prefix = usa
/><usa:usercontr ol
id=uscContr ol runat="Server"> <FONT face="Courier New" color=#000080
size=2>&nbsp; &nbsp;&nbsp;&lt ;asp:Repeater id="Repeater1" runat="server"
<STRONG>OnIte mDataBound="pro cessList"</STRONG>&gt;<BR> &nbsp;&nbsp;&nb sp;&lt;ItemTemp late&gt;<STRONG >&lt;usa:userco ntrol
runat="Server " id="uscControl "
/&gt;</STRONG>&lt;/ItemTemplate&gt ;<BR>&nbsp;&nbs p;&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>Privat e</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processLi st(</FONT><FONT color=#0000ff
size=2>ByVa l</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>ByVa l</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI .WebControls.Re peaterItemEvent Args)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp; &nbsp;&nbsp;
If</FONT><FONT size=2e.Item.It emType = ListItemType.It em </FONT><FONT
color=#0000 ff size=2>Or</FONT><FONT size=2e.Item.It emType =
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=#0000 ff
size=2>As</FONT><FONT size=2your_cont rol =
e.Item.FindCo ntrol("uscContr ol")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&n bsp;&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;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
usc.LabelSet( "test",true,"te st1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000 >&nbsp;&nbsp;&n bsp;&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="Trebuch et MS" color=#000080 size=2></FONT><BR><FONT
face="Trebuch et MS"
color=#0000 80 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto: dlozzi@(remove) delphi-ts.com"><FONT face="Trebuchet MS"
color=#0000 80 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.de lphi-ts.com</FONT></A></DIV>
<DIV>&nbsp; </DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernand ezr"
&lt;</FONT><A
href="mailto: ro************* *@gmail.com"><F ONT face="Trebuchet MS"
color=#0000 80
size=2>ro**** **********@gmai l.com</FONT></A><FONT face="Trebuchet MS"
color=#0000 80 size=2>&gt; wrote in message </FONT><A
href="news:11 *************** *******@s26g200 0cwa.googlegrou ps.com"><FONT
face="Trebuch et MS" color=#000080
size=2>news:1 1************** ********@s26g20 00cwa.googlegro ups.com</FONT></A><FONT
face="Trebuch et MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuch et MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&g t; 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:Pane l
ID="pnlRoster Profile"
runat="Server " /&gt;<BR>&gt; <BR>&gt;&nbsp ; &lt;asp:Repeate r
ID="rptRoster "
runat="Server " &gt;<BR>&gt;&nb sp;&nbsp;&nbsp;
&lt;ItemTempl ate&gt;<BR>&gt; &nbsp;&nbsp;&nb sp;
&lt;/ItemTemplate&gt ;<BR>&gt;&nbsp ; &lt;/asp:Repeater&gt ;<BR>&gt;
<BR>&gt; Code
behind:<BR>&g t; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
DataSet DS =
SQLRoutines.G etProfiles(strU ID, strCCYYS,<BR>&g t;
strRosterType );<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR >&gt;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;
{<BR>&gt; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; [User
control
here?]<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>& gt; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWire up="true"<BR>&g t; CodeFile="ucPro file.ascx.cs"
Inherits="con trols_ucRosterP rofile" %&gt;<BR>&gt;
<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ; &lt;asp:Pane l
ID="pnlProfil e"
runat="Server "&gt;<BR>&gt;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;
&lt;asp:Tab le ID="tblProfile "
runat="Server "&gt;<BR>&gt;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;
&lt;asp:Table Row&gt;<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;asp:Table Cell&gt;<BR>&gt ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;asp:Ima ge ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
&lt;asp:Table Cell&gt;<BR>&gt ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;asp:Lab el ID="lblNameDesc " runat="Server"< BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblEmailDes c" runat="Server"< BR>&gt; Font-Bold="true"
Text="Email "
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblEmployer Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Employe r"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblEmployer " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblJobTitle Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Lab el ID="lblJobTitle " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;/asp:TableRow&gt ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;/asp:Table&gt;<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:Panel&gt;<B R>&gt; <BR>&gt; <BR>&gt; user control code
behind:<BR>&g t;
<BR>&gt;&nbsp ;&nbsp;&nbsp; public void labelSet(string controlName, bool
blnVisible, string<BR>&gt; lblTitle)<BR>&g t;&nbsp;&nbsp;& nbsp;
{<BR>&gt;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp; Label
objLabel;<BR> &gt;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ; objLabel =
(Label)this.F indControl(cont rolName);<BR>&g t;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
if
(blnVisible)< BR>&gt;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
objLabel.Te xt =
lblTitle;<BR> &gt;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
else<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
objLabel.Visi ble = false;&nbsp;&nb sp;&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.7 59D9DC0--


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.comwro te in message
news:11******** **************@ k73g2000cwa.goo glegroups.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.NullRefe renceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.Fin dControl(contro lName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visibl e = false;
Source File: d:\acstest\webr oot\MOR\control s\ucRosterProfi le.ascx.cs
Line: 27

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

protected void processList(obj ect sender,
System.Web.UI.W ebControls.Repe aterItemEventAr gs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) || (e.Item.ItemTyp e
== ListItemType.Al ternatingItem))
{
controls_ucRost erProfile usc =
(controls_ucRos terProfile)e.It em.FindControl( "ucRoster") ;
usc.labelSet("t est", 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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%>

then put the user control in the repeater

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

then in the codebehind

Private Sub processList(ByV al sender As Object, ByVal e As
System.Web.UI. WebControls.Rep eaterItemEventA rgs)

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.A lternatingItem Then

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

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

usc.LabelSet("t est",true,"test 1")

'and so on

End If

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

"fernandezr " <ro************ **@gmail.comwro te in message
news:11******* *************** @s26g2000cwa.go oglegroups.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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = false;
}
Thanks,
Robert
------=_NextPart_000_ 007B_01C6A12A.7 59D9DC0
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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%'><FONT
face="Courie r New" color=#000080 size=2>%@Regist er Tagname="userco ntrol"
Tagprefix="usa " src="~/controls/usercontrol.asc x"%</FONT></A><FONT
face="Courie r 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:Repe ater id=Repeater1 OnItemDataBound ="processLis t"
runat="server" ><ITEMTEMPLATE> <?xml:namespa ce prefix = usa
/><usa:usercontr ol
id=uscContro l runat="Server"> <FONT face="Courier New" color=#000080
size=2>&nbsp;& nbsp;&nbsp;&lt; asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItem DataBound="proc essList"</STRONG>&gt;<BR> &nbsp;&nbsp;&nb sp;&lt;ItemTemp late&gt;<STRONG >&lt;usa:userco ntrol
runat="Serve r" id="uscControl "
/&gt;</STRONG>&lt;/ItemTemplate&gt ;<BR>&nbsp;&nbs p;&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=2processLi st(</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>Objec t</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.Rep eaterItemEventA rgs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;& nbsp;&nbsp;
If</FONT><FONT size=2e.Item.It emType = ListItemType.It em </FONT><FONT
color=#0000f f size=2>Or</FONT><FONT size=2e.Item.It emType =
ListItemType.A lternatingItem </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;&n bsp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_cont rol =
e.Item.FindCon trol("uscContro l")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&n bsp;&nbsp;
&nbsp;&nbsp;&n bsp; '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;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
usc.LabelSet(" test",true,"tes t1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000> &nbsp;&nbsp;&nb sp;&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="Trebuche t MS" color=#000080 size=2></FONT><BR><FONT face="Trebuchet
MS"
color=#00008 0 size=2>-- <BR>David Lozzi<BR></FONT><A
href="mailto:d lozzi@(remove)d elphi-ts.com"><FONT face="Trebuchet MS"
color=#00008 0 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.del phi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernand ezr"
&lt;</FONT><A
href="mailto:r o************** @gmail.com"><FO NT face="Trebuchet MS"
color=#00008 0
size=2>ro***** *********@gmail .com</FONT></A><FONT face="Trebuchet MS"
color=#00008 0 size=2>&gt; wrote in message </FONT><A
href="news:11* *************** ******@s26g2000 cwa.googlegroup s.com"><FONT
face="Trebuche t MS" color=#000080
size=2>news:11 *************** *******@s26g200 0cwa.googlegrou ps.com</FONT></A><FONT
face="Trebuche t MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuche t MS" color=#000080 size=2>&gt;I would like to use a user
control
as a template inside a repeater.<BR>&g t; 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:Pane l
ID="pnlRosterP rofile"
runat="Serve r" /&gt;<BR>&gt; <BR>&gt;&nbsp ; &lt;asp:Repeate r
ID="rptRoste r"
runat="Serve r" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp;
&lt;ItemTempla te&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;
&lt;/ItemTemplate&gt ;<BR>&gt;&nbsp ; &lt;/asp:Repeater&gt ;<BR>&gt;
<BR>&gt; Code
behind:<BR>&gt ; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
DataSet DS =
SQLRoutines.Ge tProfiles(strUI D, strCCYYS,<BR>&g t;
strRosterType) ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR> &gt;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
{<BR>&gt; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; [User
control
here?]<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; }<BR>&gt;
<BR>&gt;
<BR>&gt;
<BR>&gt; -------------------------------------------------<BR>&gt; User
control:<BR>&g t; <BR>&gt; &lt;%@ Control Language="C#"
AutoEventWireu p="true"<BR>&gt ; CodeFile="ucPro file.ascx.cs"
Inherits="cont rols_ucRosterPr ofile" %&gt;<BR>&gt;
<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &lt;asp:Pane l
ID="pnlProfile "
runat="Server" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
&lt;asp:Tabl e ID="tblProfile "
runat="Server" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;asp:TableR ow&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;
&lt;asp:TableC ell&gt;<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;asp:Imag e ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
&lt;asp:TableC ell&gt;<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;asp:Labe l ID="lblNameDesc " runat="Server"< BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmailDes c" runat="Server"< BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Employer "
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;/asp:TableRow&gt ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;/asp:Table&gt;<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:Panel&gt;<B R>&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>&g t;&nbsp;&nbsp;& nbsp;
{<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp; Label
objLabel;<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; objLabel =
(Label)this.Fi ndControl(contr olName);<BR>&gt ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
if
(blnVisible)<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
objLabel.Tex t =
lblTitle;<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
else<BR>&gt;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;
objLabel.Visib le = false;&nbsp;&nb sp;&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.7 59D9DC0--

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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace localhost
{
public class RepeaterTest : System.Web.UI.P age
{
protected System.Web.UI.H tmlControls.Htm lForm Form1;
protected MyRepeater myRepeater;

private void Page_Load(objec t sender, System.EventArg s e)
{
DataTable dt = new DataTable();
dt.Columns.Add( "col", typeof(string)) ;
dt.Rows.Add(new object[] {"One"});
dt.Rows.Add(new object[] {"Two"});
myRepeater.Data Source = dt.DefaultView;
myRepeater.Data Bind();
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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(EventArg s e)
{
base.OnInit (e);
base.HeaderTemp late = new LiteralTemplate ("<table>"); // Custom
Header
base.ItemTempla te = new MyTemplate(); // Custom Item Template
base.FooterTemp late = new LiteralTemplate ("</table>"); // Custom
Footer
}
}

public class MyTemplate : ITemplate
{
#region ITemplate Members

public void InstantiateIn(C ontrol container)
{
PlaceHolder Me = new PlaceHolder(); // functions as a container for
your controls
container.Contr ols.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.NamingContai ner; //
Casst to Item instance
DataRowView drv = (DataRowView) container.DataI tem; // -or-
e.Item.DataItem ;
//Repeater repeater = (Repeater) Me.NamingContai ner.NamingConta iner;
// Optionally you can access the parent repeater.
//DataView datasource = repeater.DataSo urce 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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = 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="profil e"
src="~/controls/ucRosterProfile .ascx" %>

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

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

DataSet DS = SQLRoutines.Get Rosters(int.Par se(strUID), strCCYYS,
int.Parse(strRo sterType));
rptRoster.DataS ource = DS;
rptRoster.DataB ind();

protected void processList(obj ect sender,
System.Web.UI.W ebControls.Repe aterItemEventAr gs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) || (e.Item.ItemTyp e
== ListItemType.Al ternatingItem))
{
controls_ucRost erProfile usc =
(controls_ucRos terProfile)e.It em.FindControl( "ucRoster") ;
usc.labelSet("t est", true, "test1");
}
}

--------------------------------------
user control:
<asp:Panel ID="pnlRosterPr ofile" runat="Server">
<asp:Table ID="tblRosterPr ofile" runat="Server">
<asp:TableRow >
<asp:TableCel l Width="30">
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l Width="45">

<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = 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.comwro te in message
news:11******** **************@ k73g2000cwa.goo glegroups.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.NullRefe renceException: Object reference not
set to an instance of an object.

Source Error:

Line 25: objLabel = (Label)this.Fin dControl(contro lName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visibl e = false;
Source File: d:\acstest\webr oot\MOR\control s\ucRosterProfi le.ascx.cs
Line: 27

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

protected void processList(obj ect sender,
System.Web.UI.W ebControls.Repe aterItemEventAr gs e)
{
if ((e.Item.ItemTy pe == ListItemType.It em) || (e.Item.ItemTyp e
== ListItemType.Al ternatingItem))
{
controls_ucRost erProfile usc =
(controls_ucRos terProfile)e.It em.FindControl( "ucRoster") ;
usc.labelSet("t est", 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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%>

then put the user control in the repeater

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

then in the codebehind

Private Sub processList(ByV al sender As Object, ByVal e As
System.Web.UI.W ebControls.Repe aterItemEventAr gs)

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem Then

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

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

usc.LabelSet("t est",true,"test 1")

'and so on

End If

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

"fernandezr " <ro************ **@gmail.comwro te in message
news:11******** **************@ s26g2000cwa.goo glegroups.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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = false;
}
Thanks,
Robert

------=_NextPart_000_ 007B_01C6A12A.7 59D9DC0
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="userco ntrol" Tagprefix="usa"
src="~/controls/usercontrol.asc x"%'><FONT
face="Courier New" color=#000080 size=2>%@Regist er Tagname="userco ntrol"
Tagprefix="usa" src="~/controls/usercontrol.asc x"%</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:Repea ter id=Repeater1 OnItemDataBound ="processLis t"
runat="server"> <ITEMTEMPLATE>< ?xml:namespace prefix = usa
/><usa:usercontr ol
id=uscControl runat="Server"> <FONT face="Courier New" color=#000080
size=2>&nbsp;&n bsp;&nbsp;&lt;a sp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemD ataBound="proce ssList"</STRONG>&gt;<BR> &nbsp;&nbsp;&nb sp;&lt;ItemTemp late&gt;<STRONG >&lt;usa:userco ntrol
runat="Server" id="uscControl "
/&gt;</STRONG>&lt;/ItemTemplate&gt ;<BR>&nbsp;&nbs p;&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=2processLi st(</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.W ebControls.Repe aterItemEventAr gs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&n bsp;&nbsp;
If</FONT><FONT size=2e.Item.It emType = ListItemType.It em </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.It emType =
ListItemType.Al ternatingItem </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;&n bsp;&nbsp;
&nbsp;&nbsp;&nb sp; Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_cont rol =
e.Item.FindCont rol("uscControl ")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>&nbsp;&n bsp;&nbsp;
&nbsp;&nbsp;&nb sp; '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;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
usc.LabelSet("t est",true,"test 1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000>& nbsp;&nbsp;&nbs p;&nbsp;&nbsp; &nbsp; 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>&nbsp;&n bsp;&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:dl ozzi@(remove)de lphi-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.delp hi-ts.com</FONT></A></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernand ezr"
&lt;</FONT><A
href="mailto:ro **************@ gmail.com"><FON T 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** *************** *****@s26g2000c wa.googlegroups .com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11* *************** ******@s26g2000 cwa.googlegroup s.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>&g t; 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:Pane l
ID="pnlRosterPr ofile"
runat="Server" /&gt;<BR>&gt; <BR>&gt;&nbsp ; &lt;asp:Repeate r
ID="rptRoster"
runat="Server" &gt;<BR>&gt;&nb sp;&nbsp;&nbsp;
&lt;ItemTemplat e&gt;<BR>&gt;&n bsp;&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;&nbs p;&nbsp;&nbsp;& nbsp;
DataSet DS =
SQLRoutines.Get Profiles(strUID , strCCYYS,<BR>&g t;
strRosterType); <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
foreach
(DataRow dr in
foundRows)<BR>& gt;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
{<BR>&gt; <BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; [User
control
here?]<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp; }<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="ucPro file.ascx.cs"
Inherits="contr ols_ucRosterPro file" %&gt;<BR>&gt;
<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp; &lt;asp:Pane l
ID="pnlProfile "
runat="Server"& gt;<BR>&gt;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
&lt;asp:Tabl e ID="tblProfile "
runat="Server"& gt;<BR>&gt;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;
&lt;asp:TableRo w&gt;<BR>&gt;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;asp:TableCe ll&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;asp:Imag e ID="imgPhoto" runat="Server"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
&lt;asp:TableCe ll&gt;<BR>&gt;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;asp:Labe l ID="lblNameDesc " runat="Server"< BR>&gt; Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblName" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmailDes c" runat="Server"< BR>&gt; Font-Bold="true"
Text="Email"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmail" runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Employer"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblEmployer " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle Desc" runat="Server"< BR>&gt;
Font-Bold="true"
Text="Name"
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;asp:Labe l ID="lblJobTitle " runat="Server" Text=""
/&gt;<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;br /&gt;<BR>&gt;
<BR>&gt;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;
&lt;/asp:TableCell&g t;<BR>&gt;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
&lt;/asp:TableRow&gt ;<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
&lt;/asp:Table&gt;<B R>&gt;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
&lt;/asp:Panel&gt;<B R>&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>&g t;&nbsp;&nbsp;& nbsp;
{<BR>&gt;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp; Label
objLabel;<BR>&g t;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp; objLabel =
(Label)this.Fin dControl(contro lName);<BR>&gt; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
if
(blnVisible)<BR >&gt;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;&nbsp;
objLabel.Text =
lblTitle;<BR>&g t;&nbsp;&nbsp;& nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
else<BR>&gt;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;
objLabel.Visibl e = false;&nbsp;&nb sp;&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.7 59D9DC0--
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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace localhost
{
public class RepeaterTest : System.Web.UI.P age
{
protected System.Web.UI.H tmlControls.Htm lForm Form1;
protected MyRepeater myRepeater;

private void Page_Load(objec t sender, System.EventArg s e)
{
DataTable dt = new DataTable();
dt.Columns.Add( "col", typeof(string)) ;
dt.Rows.Add(new object[] {"One"});
dt.Rows.Add(new object[] {"Two"});
myRepeater.Data Source = dt.DefaultView;
myRepeater.Data Bind();
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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(EventArg s e)
{
base.OnInit (e);
base.HeaderTemp late = new LiteralTemplate ("<table>"); // Custom
Header
base.ItemTempla te = new MyTemplate(); // Custom Item Template
base.FooterTemp late = new LiteralTemplate ("</table>"); // Custom
Footer
}
}

public class MyTemplate : ITemplate
{
#region ITemplate Members

public void InstantiateIn(C ontrol container)
{
PlaceHolder Me = new PlaceHolder(); // functions as a container for
your controls
container.Contr ols.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.NamingContai ner; //
Casst to Item instance
DataRowView drv = (DataRowView) container.DataI tem; // -or-
e.Item.DataItem ;
//Repeater repeater = (Repeater) Me.NamingContai ner.NamingConta iner;
// Optionally you can access the parent repeater.
//DataView datasource = repeater.DataSo urce 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="pnlRosterPr ofile" runat="Server" />

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

Code behind:

DataSet DS = SQLRoutines.Get Profiles(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="ucPro file.ascx.cs" Inherits="contr ols_ucRosterPro file" %>

<asp:Panel ID="pnlProfile " runat="Server">
<asp:Table ID="tblProfile " runat="Server">
<asp:TableRow >
<asp:TableCel l>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCel l>
<asp:Label ID="lblNameDesc " runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDes c" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployer Desc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer " runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitle Desc" 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.Fin dControl(contro lName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visibl e = 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
2122
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
34536
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 value> for the DropDownList What am i doing wrong? <asp:Repeater ID="Repeater1" Runat="server" OnItemDataBound="create_ddl" OnItemCommand="Repeater1_ItemCommand"> <HeaderTemplate>
2
2035
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 you re-bind the control later on in the page lifecycle, the contents are re-generated with the IDs starting from 0 again. I have written my own templated control which mostly works except that if you DataBind it more than once, the IDs don't...
0
1585
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 profile of a piece of equipment) from SQLServer. I've used a repeater on the webform since a query (i.e. by address) against the DB may return multiple machine profiles and I want to use the repeater to display as many machine profiles as are...
0
1233
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 profile of a piece of equipment) from SQLServer. I've used a repeater on the webform since a query (i.e. by address) against the DB may return multiple machine profiles and I want to use the repeater to display as many machine profiles as are...
1
318
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 color. It looks okay but I'm wondering if each of these several panels should be Web User Controls themselves. I think this would make sense, and I can figure out how to add a property that specifies the image that appears at the top of each of...
1
2107
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" ResultObject="<%#Container.DataItem%>" runat="server"> <ButtonsTemplate> <uc:ViewButton ID="ViewButton" ListingReference='<%#Eval("ListingReference")%>' runat="server" /> </ButtonsTemplate>
7
3014
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 attempting to bind a user control I've written, "ImageBox", to a repeater. The user control takes a custom property, "ContentID", that will execute a database lookup and load an image.
3
3974
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 maybe the repeater control doesn't have the capabilities...? The page needs to display a custom number of sections that appear the same but have different data..
0
11182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10886
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.