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 8 2953
First add the user control to the page declaration
<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>
then put the user control in the repeater
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate>
</asp:Repeater>
then in the codebehind
Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim usc As your_control = e.Item.FindControl("uscControl")
'now you can access all of the public properties and methods of the usercontrol, for example
usc.LabelSet("test",true,"test1")
'and so on
End If
End Sub
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.googlegr oups.com...
>I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
David,
Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27
Below is the code I'm using to call the labelSet method:
protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}
Do you know what I'm missing to get this to work?
Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
First add the user control to the page declaration
<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>
then put the user control in the repeater
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate>
</asp:Repeater>
then in the codebehind
Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim usc As your_control = e.Item.FindControl("uscControl")
'now you can access all of the public properties and methods of the usercontrol, for example
usc.LabelSet("test",true,"test1")
'and so on
End If
End Sub
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.googlegr oups.com...
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </DIV>
<DIV><FONT face="Courier New" color=#000080 size=2><</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>></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </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> <asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>><BR> <ItemTemplate& gt;<STRONG><usa:usercontrol
runat="Server" id="uscControl"
/></STRONG></ItemTemplate><BR> </asp:Repeater></FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>
Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>
'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>
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000> 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>
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> </DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr" <</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS" color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>> wrote in message </FONT><A
href="news:11**********************@s26g2000cwa.go oglegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa.g ooglegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>>I would like to use a user control
as a template inside a repeater.<BR>> Some of the fields in the control
should be hidden depending on whether<BR>> or not there is data. I'm
still a ASP .Net newbie so the way I'm going<BR>> about doing this might be a
little off. I'd appreciate some help.<BR>> <BR>> Below is the code I
have thus far but I'm not sure how to reference the<BR>> user control within
the foreach loop.<BR>> <BR>> <asp:Panel ID="pnlRosterProfile"
runat="Server" /><BR>> <BR>> <asp:Repeater ID="rptRoster"
runat="Server" ><BR>>
<ItemTemplate><BR>>
</ItemTemplate><BR>> </asp:Repeater><BR>> <BR>> Code
behind:<BR>> <BR>> &nbs p; DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>>
strRosterType);<BR>> &nb sp; DataRow[]
foundRows =
DS.Tables[0].Select();<BR>> &n bsp; foreach
(DataRow dr in foundRows)<BR>> &n bsp;
{<BR>> <BR>> &nbs p; [User control
here?]<BR>> &nbs p; }<BR>> <BR>>
<BR>> <BR>> -------------------------------------------------<BR>> User
control:<BR>> <BR>> <%@ Control Language="C#"
AutoEventWireup="true"<BR>> CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %><BR>>
<BR>> <asp:Panel ID="pnlProfile"
runat="Server"><BR>>
<asp:Table ID="tblProfile"
runat="Server"><BR>>
<asp:TableRow><BR>> &nbs p;
<asp:TableCell><BR>> &nb sp;   ;
<asp:Image ID="imgPhoto" runat="Server"
/><BR>> &n bsp;
</asp:TableCell><BR>> & nbsp; &nb sp;
<asp:TableCell><BR>> &nb sp;   ;
<asp:Label ID="lblNameDesc" runat="Server"<BR>> Font-Bold="true"
Text="Name"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblName" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmailDesc" runat="Server"<BR>> Font-Bold="true"
Text="Email"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmail" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmployerDesc" runat="Server"<BR>> Font-Bold="true"
Text="Employer"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmployer" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblJobTitleDesc" runat="Server"<BR>> Font-Bold="true"
Text="Name"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblJobTitle" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br /><BR>>
<BR>>
</asp:TableCell><BR>> & nbsp;
</asp:TableRow><BR>> &n bsp;
</asp:Table><BR>>   ;
</asp:Panel><BR>> <BR>> <BR>> user control code behind:<BR>>
<BR>> public void labelSet(string controlName, bool
blnVisible, string<BR>> lblTitle)<BR>>
{<BR>>   ; Label
objLabel;<BR>> &nb sp; objLabel =
(Label)this.FindControl(controlName);<BR>>  ;
if
(blnVisible)<BR>>
objLabel.Text = lblTitle;<BR>> &nb sp;
else<BR>> &n bsp;
objLabel.Visible = false; <BR>> }<BR>>
<BR>> <BR>> Thanks,<BR>> Robert<BR>></FONT></BODY></HTML>
------=_NextPart_000_007B_01C6A12A.759D9DC0--
Line 27: objLabel.Text = lblTitle;
I believe should be
Line 27: objLabel.Text = lblTitle.text;
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
David,
Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27
Below is the code I'm using to call the labelSet method:
protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}
Do you know what I'm missing to get this to work?
Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
>First add the user control to the page declaration
<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>
then put the user control in the repeater
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList"> <ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate> </asp:Repeater>
then in the codebehind
Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs )
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim usc As your_control = e.Item.FindControl("uscControl")
'now you can access all of the public properties and methods of the usercontrol, for example
usc.LabelSet("test",true,"test1")
'and so on
End If
End Sub
-- David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.googleg roups.com...
>I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Google-AttachSize: 11079
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <META content="MSHTML 6.00.5346.5" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY> <DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user control to the page declaration</FONT></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </DIV> <DIV><FONT face="Courier New" color=#000080 size=2><</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>></FONT></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </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> <asp:Repeater id="Repeater1" runat="server" <STRONG>OnItemDataBound="processList"</STRONG>><BR> <ItemTemplate& gt;<STRONG><usa:usercontrol runat="Server" id="uscControl" /></STRONG></ItemTemplate><BR> </asp:Repeater></FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New"> System.Web.UI.WebControls.RepeaterItemEventArgs )</FONT></P> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType = ListItemType.AlternatingItem </FONT><FONT color=#0000ff size=2>Then</P></FONT></FONT><FONT size=2> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2your_control = e.Item.FindControl("uscControl")</FONT></FONT></P> <P><FONT face="Courier New" color=#008000 size=2> '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> usc.LabelSet("test",true,"test1")</FONT></P> <P><FONT size=2><FONT face="Courier New" color=#008000> 'and so on</FONT></P> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> 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> </DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr" <</FONT><A href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS" color=#000080 size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS" color=#000080 size=2>> wrote in message </FONT><A href="news:11**********************@s26g2000cwa.g ooglegroups.com"><FONT face="Trebuchet MS" color=#000080 size=2>news:11**********************@s26g2000cwa. googlegroups.com</FONT></A><FONT face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT face="Trebuchet MS" color=#000080 size=2>>I would like to use a user control as a template inside a repeater.<BR>> Some of the fields in the control should be hidden depending on whether<BR>> or not there is data. I'm still a ASP .Net newbie so the way I'm going<BR>> about doing this might be a little off. I'd appreciate some help.<BR>> <BR>> Below is the code I have thus far but I'm not sure how to reference the<BR>> user control within the foreach loop.<BR>> <BR>> <asp:Panel ID="pnlRosterProfile" runat="Server" /><BR>> <BR>> <asp:Repeater ID="rptRoster" runat="Server" ><BR>> <ItemTemplate><BR>> </ItemTemplate><BR>> </asp:Repeater><BR>> <BR>> Code behind:<BR>> <BR>> &nbs p; DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>> strRosterType);<BR>> &n bsp; DataRow[] foundRows = DS.Tables[0].Select();<BR>> &n bsp; foreach (DataRow dr in foundRows)<BR>> & nbsp; {<BR>> <BR>> &nbs p; [User control here?]<BR>> &nbs p; }<BR>> <BR>> <BR>> <BR>> -------------------------------------------------<BR>> User control:<BR>> <BR>> <%@ Control Language="C#" AutoEventWireup="true"<BR>> CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %><BR>> <BR>> <asp:Panel ID="pnlProfile" runat="Server"><BR>>   ; <asp:Table ID="tblProfile" runat="Server"><BR>>   ; <asp:TableRow><BR>> &nb sp;   ; <asp:TableCell><BR>> &n bsp; &nbs p; <asp:Image ID="imgPhoto" runat="Server" /><BR>> &n bsp; </asp:TableCell><BR>> & nbsp; &nb sp; <asp:TableCell><BR>> &n bsp; &nbs p; <asp:Label ID="lblNameDesc" runat="Server"<BR>> Font-Bold="true" Text="Name" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblName" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmailDesc" runat="Server"<BR>> Font-Bold="true" Text="Email" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmail" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmployerDesc" runat="Server"<BR>> Font-Bold="true" Text="Employer" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmployer" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblJobTitleDesc" runat="Server"<BR>> Font-Bold="true" Text="Name" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblJobTitle" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> <BR>>   ; </asp:TableCell><BR>> & nbsp; </asp:TableRow><BR>> &n bsp; </asp:Table><BR>>   ; </asp:Panel><BR>> <BR>> <BR>> user control code behind:<BR>> <BR>> public void labelSet(string controlName, bool blnVisible, string<BR>> lblTitle)<BR>> {<BR>> &nbs p; Label objLabel;<BR>> &n bsp; objLabel = (Label)this.FindControl(controlName);<BR>>&nbs p; if (blnVisible)<BR>>   ; objLabel.Text = lblTitle;<BR>> &n bsp; else<BR>> & nbsp; objLabel.Visible = false; <BR>> }<BR>> <BR>> <BR>> Thanks,<BR>> Robert<BR>></FONT></BODY></HTML>
------=_NextPart_000_007B_01C6A12A.759D9DC0--
My apologies, that is wrong. I misread your code. I thought lblTitle was a
Label, but its a string variable. Anyway, my thoughts on the error is that
this line
>Line 25: objLabel = (Label)this.FindControl(controlName);
is the problem. I'm not familiar with C# at all, i'm a VB developer ;). Is
"this" a valid reference in C#? it isnt in VB, VB uses "me". I don't know
where to go from here without viewing all of the actual code.
good luck
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"David Lozzi" <dl****@nospam.nospamwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
>Line 27: objLabel.Text = lblTitle;
I believe should be
>Line 27: objLabel.Text = lblTitle.text;
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
>David,
Thank you! That helped quite a bit. I can now get the user control to be called multiple times and the template html is displayed. Now I'm getting an error when I try to reference a method inside of the user control.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 25: objLabel = (Label)this.FindControl(controlName); Line 26: if (blnVisible) Line 27: objLabel.Text = lblTitle; Line 28: else Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs Line: 27
Below is the code I'm using to call the labelSet method:
protected void processList(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) { if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) { controls_ucRosterProfile usc = (controls_ucRosterProfile)e.Item.FindControl("ucR oster"); usc.labelSet("test", true, "test1"); } }
Do you know what I'm missing to get this to work?
Thanks, Happy Thursday, Robert
David Lozzi wrote:
>>First add the user control to the page declaration
<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>
then put the user control in the repeater
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList"> <ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate> </asp:Repeater>
then in the codebehind
Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArg s)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim usc As your_control = e.Item.FindControl("uscControl")
'now you can access all of the public properties and methods of the usercontrol, for example
usc.LabelSet("test",true,"test1")
'and so on
End If
End Sub
-- David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.google groups.com... I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the way I'm going about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" > <ItemTemplate> </ItemTemplate> </asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS, strRosterType); DataRow[] foundRows = DS.Tables[0].Select(); foreach (DataRow dr in foundRows) {
[User control here?] } ------------------------------------------------- User control:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server"> <asp:Table ID="tblProfile" runat="Server"> <asp:TableRow> <asp:TableCell> <asp:Image ID="imgPhoto" runat="Server" /> </asp:TableCell> <asp:TableCell> <asp:Label ID="lblNameDesc" runat="Server" Font-Bold="true" Text="Name" /> <asp:Label ID="lblName" runat="Server" Text="" /> <br /> <asp:Label ID="lblEmailDesc" runat="Server" Font-Bold="true" Text="Email" /> <asp:Label ID="lblEmail" runat="Server" Text="" /> <br /> <asp:Label ID="lblEmployerDesc" runat="Server" Font-Bold="true" Text="Employer" /> <asp:Label ID="lblEmployer" runat="Server" Text="" /> <br /> <asp:Label ID="lblJobTitleDesc" runat="Server" Font-Bold="true" Text="Name" /> <asp:Label ID="lblJobTitle" runat="Server" Text="" /> <br />
</asp:TableCell> </asp:TableRow> </asp:Table> </asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string lblTitle) { Label objLabel; objLabel = (Label)this.FindControl(controlName); if (blnVisible) objLabel.Text = lblTitle; else objLabel.Visible = false; }
Thanks, Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Google-AttachSize: 11079
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <META content="MSHTML 6.00.5346.5" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY> <DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user control to the page declaration</FONT></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </DIV> <DIV><FONT face="Courier New" color=#000080 size=2><</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>></FONT></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </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> <asp:Repeater id="Repeater1" runat="server" <STRONG>OnItemDataBound="processList"</STRONG>><BR> <ItemTemplate& gt;<STRONG><usa:usercontrol runat="Server" id="uscControl" /></STRONG></ItemTemplate><BR> </asp:Repeater></FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New"> System.Web.UI.WebControls.RepeaterItemEventArgs) </FONT></P> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType = ListItemType.AlternatingItem </FONT><FONT color=#0000ff size=2>Then</P></FONT></FONT><FONT size=2> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2your_control = e.Item.FindControl("uscControl")</FONT></FONT></P> <P><FONT face="Courier New" color=#008000 size=2> '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> usc.LabelSet("test",true,"test1")</FONT></P> <P><FONT size=2><FONT face="Courier New" color=#008000> 'and so on</FONT></P> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> 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> </DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr" <</FONT><A href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS" color=#000080 size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS" color=#000080 size=2>> wrote in message </FONT><A href="news:11**********************@s26g2000cwa. googlegroups.com"><FONT face="Trebuchet MS" color=#000080 size=2>news:11**********************@s26g2000cwa .googlegroups.com</FONT></A><FONT face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT face="Trebuchet MS" color=#000080 size=2>>I would like to use a user control as a template inside a repeater.<BR>> Some of the fields in the control should be hidden depending on whether<BR>> or not there is data. I'm still a ASP .Net newbie so the way I'm going<BR>> about doing this might be a little off. I'd appreciate some help.<BR>> <BR>> Below is the code I have thus far but I'm not sure how to reference the<BR>> user control within the foreach loop.<BR>> <BR>> <asp:Panel ID="pnlRosterProfile" runat="Server" /><BR>> <BR>> <asp:Repeater ID="rptRoster" runat="Server" ><BR>> <ItemTemplate><BR>> </ItemTemplate><BR>> </asp:Repeater><BR>> <BR>> Code behind:<BR>> <BR>> &nbs p; DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>> strRosterType);<BR>> & nbsp; DataRow[] foundRows = DS.Tables[0].Select();<BR>> &n bsp; foreach (DataRow dr in foundRows)<BR>> {<BR>> <BR>> &nbs p; [User control here?]<BR>> &nbs p; }<BR>> <BR>> <BR>> <BR>> -------------------------------------------------<BR>> User control:<BR>> <BR>> <%@ Control Language="C#" AutoEventWireup="true"<BR>> CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %><BR>> <BR>> <asp:Panel ID="pnlProfile" runat="Server"><BR>> &nbs p; <asp:Table ID="tblProfile" runat="Server"><BR>> &nbs p; <asp:TableRow><BR>> &n bsp; &nbs p; <asp:TableCell><BR>> & nbsp; &nb sp; <asp:Image ID="imgPhoto" runat="Server" /><BR>> &n bsp; </asp:TableCell><BR>> & nbsp; &nb sp; <asp:TableCell><BR>> & nbsp; &nb sp; <asp:Label ID="lblNameDesc" runat="Server"<BR>> Font-Bold="true" Text="Name" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblName" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmailDesc" runat="Server"<BR>> Font-Bold="true" Text="Email" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmail" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmployerDesc" runat="Server"<BR>> Font-Bold="true" Text="Employer" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmployer" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblJobTitleDesc" runat="Server"<BR>> Font-Bold="true" Text="Name" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblJobTitle" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> <BR>> &nbs p; </asp:TableCell><BR>> & nbsp; </asp:TableRow><BR>> &n bsp; </asp:Table><BR>>   ; </asp:Panel><BR>> <BR>> <BR>> user control code behind:<BR>> <BR>> public void labelSet(string controlName, bool blnVisible, string<BR>> lblTitle)<BR>> {<BR>> &nb sp; Label objLabel;<BR>> & nbsp; objLabel = (Label)this.FindControl(controlName);<BR>>&nb sp; if (blnVisible)<BR>> &nbs p; objLabel.Text = lblTitle;<BR>> & nbsp; else<BR>> objLabel.Visible = false; <BR>> }<BR>> <BR>> <BR>> Thanks,<BR>> Robert<BR>></FONT></BODY></HTML>
------=_NextPart_000_007B_01C6A12A.759D9DC0--
After further review of your code, I think you're doing too much work. What
are you trying to accomplish?
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
David,
Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27
Below is the code I'm using to call the labelSet method:
protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}
Do you know what I'm missing to get this to work?
Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
>First add the user control to the page declaration
<%@Register Tagname="usercontrol" Tagprefix="usa" src="~/controls/usercontrol.ascx"%>
then put the user control in the repeater
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="processList"> <ItemTemplate><usa:usercontrol runat="Server" id="uscControl" /></ItemTemplate> </asp:Repeater>
then in the codebehind
Private Sub processList(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs )
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim usc As your_control = e.Item.FindControl("uscControl")
'now you can access all of the public properties and methods of the usercontrol, for example
usc.LabelSet("test",true,"test1")
'and so on
End If
End Sub
-- David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message news:11**********************@s26g2000cwa.googleg roups.com...
>I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Google-AttachSize: 11079
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <META content="MSHTML 6.00.5346.5" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY> <DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user control to the page declaration</FONT></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </DIV> <DIV><FONT face="Courier New" color=#000080 size=2><</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>></FONT></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </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> <asp:Repeater id="Repeater1" runat="server" <STRONG>OnItemDataBound="processList"</STRONG>><BR> <ItemTemplate& gt;<STRONG><usa:usercontrol runat="Server" id="uscControl" /></STRONG></ItemTemplate><BR> </asp:Repeater></FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New"> System.Web.UI.WebControls.RepeaterItemEventArgs )</FONT></P> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType = ListItemType.AlternatingItem </FONT><FONT color=#0000ff size=2>Then</P></FONT></FONT><FONT size=2> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2your_control = e.Item.FindControl("uscControl")</FONT></FONT></P> <P><FONT face="Courier New" color=#008000 size=2> '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> usc.LabelSet("test",true,"test1")</FONT></P> <P><FONT size=2><FONT face="Courier New" color=#008000> 'and so on</FONT></P> <P></FONT><FONT face="Courier New"><FONT color=#0000ff size=2> 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> </DIV> <DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr" <</FONT><A href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS" color=#000080 size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS" color=#000080 size=2>> wrote in message </FONT><A href="news:11**********************@s26g2000cwa.g ooglegroups.com"><FONT face="Trebuchet MS" color=#000080 size=2>news:11**********************@s26g2000cwa. googlegroups.com</FONT></A><FONT face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT face="Trebuchet MS" color=#000080 size=2>>I would like to use a user control as a template inside a repeater.<BR>> Some of the fields in the control should be hidden depending on whether<BR>> or not there is data. I'm still a ASP .Net newbie so the way I'm going<BR>> about doing this might be a little off. I'd appreciate some help.<BR>> <BR>> Below is the code I have thus far but I'm not sure how to reference the<BR>> user control within the foreach loop.<BR>> <BR>> <asp:Panel ID="pnlRosterProfile" runat="Server" /><BR>> <BR>> <asp:Repeater ID="rptRoster" runat="Server" ><BR>> <ItemTemplate><BR>> </ItemTemplate><BR>> </asp:Repeater><BR>> <BR>> Code behind:<BR>> <BR>> &nbs p; DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>> strRosterType);<BR>> &n bsp; DataRow[] foundRows = DS.Tables[0].Select();<BR>> &n bsp; foreach (DataRow dr in foundRows)<BR>> & nbsp; {<BR>> <BR>> &nbs p; [User control here?]<BR>> &nbs p; }<BR>> <BR>> <BR>> <BR>> -------------------------------------------------<BR>> User control:<BR>> <BR>> <%@ Control Language="C#" AutoEventWireup="true"<BR>> CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %><BR>> <BR>> <asp:Panel ID="pnlProfile" runat="Server"><BR>>   ; <asp:Table ID="tblProfile" runat="Server"><BR>>   ; <asp:TableRow><BR>> &nb sp;   ; <asp:TableCell><BR>> &n bsp; &nbs p; <asp:Image ID="imgPhoto" runat="Server" /><BR>> &n bsp; </asp:TableCell><BR>> & nbsp; &nb sp; <asp:TableCell><BR>> &n bsp; &nbs p; <asp:Label ID="lblNameDesc" runat="Server"<BR>> Font-Bold="true" Text="Name" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblName" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmailDesc" runat="Server"<BR>> Font-Bold="true" Text="Email" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmail" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmployerDesc" runat="Server"<BR>> Font-Bold="true" Text="Employer" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblEmployer" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> &n bsp; &nbs p; <asp:Label ID="lblJobTitleDesc" runat="Server"<BR>> Font-Bold="true" Text="Name" /><BR>> &n bsp; &nbs p; <asp:Label ID="lblJobTitle" runat="Server" Text="" /><BR>> &n bsp; &nbs p; <br /><BR>> <BR>>   ; </asp:TableCell><BR>> & nbsp; </asp:TableRow><BR>> &n bsp; </asp:Table><BR>>   ; </asp:Panel><BR>> <BR>> <BR>> user control code behind:<BR>> <BR>> public void labelSet(string controlName, bool blnVisible, string<BR>> lblTitle)<BR>> {<BR>> &nbs p; Label objLabel;<BR>> &n bsp; objLabel = (Label)this.FindControl(controlName);<BR>>&nbs p; if (blnVisible)<BR>>   ; objLabel.Text = lblTitle;<BR>> &n bsp; else<BR>> & nbsp; objLabel.Visible = false; <BR>> }<BR>> <BR>> <BR>> Thanks,<BR>> Robert<BR>></FONT></BODY></HTML>
------=_NextPart_000_007B_01C6A12A.759D9DC0--
There are many possible solutions, of which I favor the following:
Create a class which implements ITemplate.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace localhost
{
public class RepeaterTest : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected MyRepeater myRepeater;
private void Page_Load(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col", typeof(string));
dt.Rows.Add(new object[] {"One"});
dt.Rows.Add(new object[] {"Two"});
myRepeater.DataSource = dt.DefaultView;
myRepeater.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
myRepeater = new MyRepeater();
Form1.Controls.Add(myRepeater);
}
#endregion
}
// One way to specify your template is to inherit from Repeater, but
you can also set the Template as a property before DataBind()
public class MyRepeater : Repeater
{
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
base.HeaderTemplate = new LiteralTemplate("<table>"); // Custom
Header
base.ItemTemplate = new MyTemplate(); // Custom Item Template
base.FooterTemplate = new LiteralTemplate("</table>"); // Custom
Footer
}
}
public class MyTemplate : ITemplate
{
#region ITemplate Members
public void InstantiateIn(Control container)
{
PlaceHolder Me = new PlaceHolder(); // functions as a container for
your controls
container.Controls.Add(Me); // Add to Controls collection
Me.DataBinding += new EventHandler(Me_DataBinding); // Bind to
DataBinding event
}
#endregion
// For each iteration of data do the following
private void Me_DataBinding(object sender, EventArgs e)
{
PlaceHolder Me = (PlaceHolder) sender; // Cast back to container
control
RepeaterItem container = (RepeaterItem) Me.NamingContainer; //
Casst to Item instance
DataRowView drv = (DataRowView) container.DataItem; // -or-
e.Item.DataItem;
//Repeater repeater = (Repeater) Me.NamingContainer.NamingContainer;
// Optionally you can access the parent repeater.
//DataView datasource = repeater.DataSource as DataView; //
Optionally you can access the original datasource.
Me.Controls.Add(new
LiteralControl(String.Format("<tr><td>{0}</td></tr>", drv[0]))); //
Add your control / content.
}
}
}
fernandezr wrote:
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
David,
After trying to implement your code I simplified mine. Hopefully this
will clear things up about what I'm trying to do. I have a user
control that will display for each record returned by the dataset. If
fields are blank I will hide those rows in the template for that
record. I am planning on reusing this control on several pages.
<%@ Register TagPrefix="uc1" TagName="profile"
src="~/controls/ucRosterProfile.ascx" %>
<asp:Repeater ID="rptRoster" runat="Server"
OnItemDataBound="processList" >
<ItemTemplate>
<uc1:profile runat="Server" id="ucRoster" />
</ItemTemplate>
</asp:Repeater>
-------------
code behind
DataSet DS = SQLRoutines.GetRosters(int.Parse(strUID), strCCYYS,
int.Parse(strRosterType));
rptRoster.DataSource = DS;
rptRoster.DataBind();
protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}
--------------------------------------
user control:
<asp:Panel ID="pnlRosterProfile" runat="Server">
<asp:Table ID="tblRosterProfile" runat="Server">
<asp:TableRow>
<asp:TableCell Width="30">
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell Width="45">
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
-----
code behind
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Happy Friday,
Robert
David Lozzi wrote:
After further review of your code, I think you're doing too much work. What
are you trying to accomplish?
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
David,
Thank you! That helped quite a bit. I can now get the user control to
be called multiple times and the template html is displayed. Now I'm
getting an error when I try to reference a method inside of the user
control.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 25: objLabel = (Label)this.FindControl(controlName);
Line 26: if (blnVisible)
Line 27: objLabel.Text = lblTitle;
Line 28: else
Line 29: objLabel.Visible = false;
Source File: d:\acstest\webroot\MOR\controls\ucRosterProfile.as cx.cs
Line: 27
Below is the code I'm using to call the labelSet method:
protected void processList(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
controls_ucRosterProfile usc =
(controls_ucRosterProfile)e.Item.FindControl("ucRo ster");
usc.labelSet("test", true, "test1");
}
}
Do you know what I'm missing to get this to work?
Thanks,
Happy Thursday,
Robert
David Lozzi wrote:
First add the user control to the page declaration
<%@Register Tagname="usercontrol" Tagprefix="usa"
src="~/controls/usercontrol.ascx"%>
then put the user control in the repeater
<asp:Repeater id="Repeater1" runat="server"
OnItemDataBound="processList">
<ItemTemplate><usa:usercontrol runat="Server" id="uscControl"
/></ItemTemplate>
</asp:Repeater>
then in the codebehind
Private Sub processList(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim usc As your_control = e.Item.FindControl("uscControl")
'now you can access all of the public properties and methods of
the usercontrol, for example
usc.LabelSet("test",true,"test1")
'and so on
End If
End Sub
--
David Lozzi dlozzi@(remove)delphi-ts.com www.delphi-ts.com
"fernandezr" <ro**************@gmail.comwrote in message
news:11**********************@s26g2000cwa.googlegr oups.com...
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
------=_NextPart_000_007B_01C6A12A.759D9DC0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 11079
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5346.5" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>First add the user
control
to the page declaration</FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </DIV>
<DIV><FONT face="Courier New" color=#000080 size=2><</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>></FONT></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </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> <asp:Repeater id="Repeater1" runat="server"
<STRONG>OnItemDataBound="processList"</STRONG>><BR> <ItemTemplate& gt;<STRONG><usa:usercontrol
runat="Server" id="uscControl"
/></STRONG></ItemTemplate><BR> </asp:Repeater></FONT></usa:usercontrol></ITEMTEMPLATE></asp:Repeater></DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2></FONT> </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> </DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>Private</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Sub</FONT><FONT size=2processList(</FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2sender </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2</FONT><FONT color=#0000ff
size=2>Object</FONT><FONT size=2>, </FONT><FONT color=#0000ff
size=2>ByVal</FONT><FONT size=2e </FONT><FONT color=#0000ff
size=2>As</FONT></FONT><FONT size=2><FONT face="Courier New">
System.Web.UI.WebControls.RepeaterItemEventArgs)</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>
If</FONT><FONT size=2e.Item.ItemType = ListItemType.Item </FONT><FONT
color=#0000ff size=2>Or</FONT><FONT size=2e.Item.ItemType =
ListItemType.AlternatingItem </FONT><FONT color=#0000ff
size=2>Then</P></FONT></FONT><FONT size=2>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>
Dim</FONT><FONT size=2usc </FONT><FONT color=#0000ff
size=2>As</FONT><FONT size=2your_control =
e.Item.FindControl("uscControl")</FONT></FONT></P>
<P><FONT face="Courier New" color=#008000 size=2>
'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>
usc.LabelSet("test",true,"test1")</FONT></P>
<P><FONT size=2><FONT face="Courier New"
color=#008000> 'and so on</FONT></P>
<P></FONT><FONT face="Courier New"><FONT color=#0000ff
size=2>
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> </DIV>
<DIV><FONT face="Trebuchet MS" color=#000080 size=2>"fernandezr"
<</FONT><A
href="mailto:ro**************@gmail.com"><FONT face="Trebuchet MS"
color=#000080
size=2>ro**************@gmail.com</FONT></A><FONT face="Trebuchet MS"
color=#000080 size=2>> wrote in message </FONT><A
href="news:11**********************@s26g2000cwa.go oglegroups.com"><FONT
face="Trebuchet MS" color=#000080
size=2>news:11**********************@s26g2000cwa.g ooglegroups.com</FONT></A><FONT
face="Trebuchet MS" color=#000080 size=2>...</FONT></DIV><FONT
face="Trebuchet MS" color=#000080 size=2>>I would like to use a user
control
as a template inside a repeater.<BR>> Some of the fields in the
control
should be hidden depending on whether<BR>> or not there is data.
I'm
still a ASP .Net newbie so the way I'm going<BR>> about doing this
might be a
little off. I'd appreciate some help.<BR>> <BR>> Below is the
code I
have thus far but I'm not sure how to reference the<BR>> user control
within
the foreach loop.<BR>> <BR>> <asp:Panel
ID="pnlRosterProfile"
runat="Server" /><BR>> <BR>> <asp:Repeater
ID="rptRoster"
runat="Server" ><BR>>
<ItemTemplate><BR>>
</ItemTemplate><BR>> </asp:Repeater><BR>>
<BR>> Code
behind:<BR>> <BR>> &nbs p;
DataSet DS =
SQLRoutines.GetProfiles(strUID, strCCYYS,<BR>>
strRosterType);<BR>> &nb sp;
DataRow[]
foundRows =
DS.Tables[0].Select();<BR>> &n bsp;
foreach
(DataRow dr in
foundRows)<BR>> &n bsp;
{<BR>> <BR>> &nbs p; [User
control
here?]<BR>> &nbs p; }<BR>>
<BR>>
<BR>>
<BR>> -------------------------------------------------<BR>> User
control:<BR>> <BR>> <%@ Control Language="C#"
AutoEventWireup="true"<BR>> CodeFile="ucProfile.ascx.cs"
Inherits="controls_ucRosterProfile" %><BR>>
<BR>> <asp:Panel
ID="pnlProfile"
runat="Server"><BR>>
<asp:Table ID="tblProfile"
runat="Server"><BR>>
<asp:TableRow><BR>> &nbs p;
<asp:TableCell><BR>> &nb sp;   ;
<asp:Image ID="imgPhoto" runat="Server"
/><BR>> &n bsp;
</asp:TableCell><BR>> & nbsp; &nb sp;
<asp:TableCell><BR>> &nb sp;   ;
<asp:Label ID="lblNameDesc" runat="Server"<BR>> Font-Bold="true"
Text="Name"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblName" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmailDesc" runat="Server"<BR>> Font-Bold="true"
Text="Email"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmail" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmployerDesc" runat="Server"<BR>>
Font-Bold="true"
Text="Employer"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblEmployer" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblJobTitleDesc" runat="Server"<BR>>
Font-Bold="true"
Text="Name"
/><BR>> &n bsp; &nbs p;
<asp:Label ID="lblJobTitle" runat="Server" Text=""
/><BR>> &n bsp; &nbs p;
<br /><BR>>
<BR>>
</asp:TableCell><BR>> & nbsp;
</asp:TableRow><BR>> &n bsp;
</asp:Table><BR>>   ;
</asp:Panel><BR>> <BR>> <BR>> user control code
behind:<BR>>
<BR>> public void labelSet(string controlName, bool
blnVisible, string<BR>> lblTitle)<BR>>
{<BR>>   ; Label
objLabel;<BR>> &nb sp; objLabel =
(Label)this.FindControl(controlName);<BR>>  ;
if
(blnVisible)<BR>>
objLabel.Text =
lblTitle;<BR>> &nb sp;
else<BR>> &n bsp;
objLabel.Visible = false;
<BR>> }<BR>>
<BR>> <BR>> Thanks,<BR>> Robert<BR>></FONT></BODY></HTML>
------=_NextPart_000_007B_01C6A12A.759D9DC0--
Could you tell me why you favor this solution over the others? I have
seen a few articles where people mention different solutions, like you
say, it is hard to figure out which would be the best road to take. I
would like the solution to be reusable and efficient.
Thanks,
Robert ka*******@gmail.com wrote:
There are many possible solutions, of which I favor the following:
Create a class which implements ITemplate.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace localhost
{
public class RepeaterTest : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected MyRepeater myRepeater;
private void Page_Load(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col", typeof(string));
dt.Rows.Add(new object[] {"One"});
dt.Rows.Add(new object[] {"Two"});
myRepeater.DataSource = dt.DefaultView;
myRepeater.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
myRepeater = new MyRepeater();
Form1.Controls.Add(myRepeater);
}
#endregion
}
// One way to specify your template is to inherit from Repeater, but
you can also set the Template as a property before DataBind()
public class MyRepeater : Repeater
{
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
base.HeaderTemplate = new LiteralTemplate("<table>"); // Custom
Header
base.ItemTemplate = new MyTemplate(); // Custom Item Template
base.FooterTemplate = new LiteralTemplate("</table>"); // Custom
Footer
}
}
public class MyTemplate : ITemplate
{
#region ITemplate Members
public void InstantiateIn(Control container)
{
PlaceHolder Me = new PlaceHolder(); // functions as a container for
your controls
container.Controls.Add(Me); // Add to Controls collection
Me.DataBinding += new EventHandler(Me_DataBinding); // Bind to
DataBinding event
}
#endregion
// For each iteration of data do the following
private void Me_DataBinding(object sender, EventArgs e)
{
PlaceHolder Me = (PlaceHolder) sender; // Cast back to container
control
RepeaterItem container = (RepeaterItem) Me.NamingContainer; //
Casst to Item instance
DataRowView drv = (DataRowView) container.DataItem; // -or-
e.Item.DataItem;
//Repeater repeater = (Repeater) Me.NamingContainer.NamingContainer;
// Optionally you can access the parent repeater.
//DataView datasource = repeater.DataSource as DataView; //
Optionally you can access the original datasource.
Me.Controls.Add(new
LiteralControl(String.Format("<tr><td>{0}</td></tr>", drv[0]))); //
Add your control / content.
}
}
}
fernandezr wrote:
I would like to use a user control as a template inside a repeater.
Some of the fields in the control should be hidden depending on whether
or not there is data. I'm still a ASP .Net newbie so the way I'm going
about doing this might be a little off. I'd appreciate some help.
Below is the code I have thus far but I'm not sure how to reference the
user control within the foreach loop.
<asp:Panel ID="pnlRosterProfile" runat="Server" />
<asp:Repeater ID="rptRoster" runat="Server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataSet DS = SQLRoutines.GetProfiles(strUID, strCCYYS,
strRosterType);
DataRow[] foundRows = DS.Tables[0].Select();
foreach (DataRow dr in foundRows)
{
[User control here?]
}
-------------------------------------------------
User control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ucProfile.ascx.cs" Inherits="controls_ucRosterProfile" %>
<asp:Panel ID="pnlProfile" runat="Server">
<asp:Table ID="tblProfile" runat="Server">
<asp:TableRow>
<asp:TableCell>
<asp:Image ID="imgPhoto" runat="Server" />
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblNameDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblName" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmailDesc" runat="Server"
Font-Bold="true" Text="Email" />
<asp:Label ID="lblEmail" runat="Server" Text="" />
<br />
<asp:Label ID="lblEmployerDesc" runat="Server"
Font-Bold="true" Text="Employer" />
<asp:Label ID="lblEmployer" runat="Server" Text="" />
<br />
<asp:Label ID="lblJobTitleDesc" runat="Server"
Font-Bold="true" Text="Name" />
<asp:Label ID="lblJobTitle" runat="Server" Text="" />
<br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
user control code behind:
public void labelSet(string controlName, bool blnVisible, string
lblTitle)
{
Label objLabel;
objLabel = (Label)this.FindControl(controlName);
if (blnVisible)
objLabel.Text = lblTitle;
else
objLabel.Visible = false;
}
Thanks,
Robert
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |