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

F

fernandezr

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


Thanks,
Robert
 
D

David Lozzi

First add the user control to the page declaration

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

then put the user control in the repeater

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

then in the codebehind

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

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

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

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

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

'and so on

End If

End Sub


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

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


Thanks,
Robert
 
F

fernandezr

David,

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

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

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;


Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.ascx.cs
Line: 27

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

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

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

Thanks,
Happy Thursday,
Robert


David said:
First add the user control to the page declaration

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

then put the user control in the repeater

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

then in the codebehind

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

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

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

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

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

'and so on

End If

End Sub


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

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


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

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

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

David Lozzi

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 said:
David,

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

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

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;


Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.ascx.cs
Line: 27

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

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

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

Thanks,
Happy Thursday,
Robert


David said:
First add the user control to the page declaration

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

then put the user control in the repeater

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

then in the codebehind

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

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

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

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

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

'and so on

End If

End Sub


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

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


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

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

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

David Lozzi

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

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 said:
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 said:
David,

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

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

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;


Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.ascx.cs
Line: 27

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

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

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

Thanks,
Happy Thursday,
Robert


David said:
First add the user control to the page declaration

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

then put the user control in the repeater

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

then in the codebehind

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

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

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

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

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

'and so on

End If

End Sub


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

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


Thanks,
Robert

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

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

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

David Lozzi

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 said:
David,

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

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

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;


Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.ascx.cs
Line: 27

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

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

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

Thanks,
Happy Thursday,
Robert


David said:
First add the user control to the page declaration

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

then put the user control in the repeater

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

then in the codebehind

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

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

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

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

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

'and so on

End If

End Sub


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

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


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

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

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

karldirck

There are many possible solutions, of which I favor the following:
Create a class which implements ITemplate.

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

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

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

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

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

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

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

public class MyTemplate : ITemplate
{
#region ITemplate Members

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

#endregion

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

Me.Controls.Add(new
LiteralControl(String.Format("<tr><td>{0}</td></tr>", drv[0]))); //
Add your control / content.
}
}
}
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


Thanks,
Robert
 
F

fernandezr

David,

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

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

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

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

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

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

--------------------------------------
user control:


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

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

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

-----
code behind

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

Thanks,
Happy Friday,
Robert


David said:
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 said:
David,

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

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

Source Error:

Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;


Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.ascx.cs
Line: 27

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

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

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

Thanks,
Happy Thursday,
Robert


David said:
First add the user control to the page declaration

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

then put the user control in the repeater

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

then in the codebehind

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

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

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

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

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

'and so on

End If

End Sub


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

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

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


Thanks,
Robert

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

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

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

fernandezr

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


There are many possible solutions, of which I favor the following:
Create a class which implements ITemplate.

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

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

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

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

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

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

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

public class MyTemplate : ITemplate
{
#region ITemplate Members

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

#endregion

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

Me.Controls.Add(new
LiteralControl(String.Format("<tr><td>{0}</td></tr>", drv[0]))); //
Add your control / content.
}
}
}
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.

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

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

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

Code behind:

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

[User control here?]
}



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

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

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

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


user control code behind:

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


Thanks,
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top