Jump to content

DataList Help


ewhitmor

Recommended Posts

I have been searching and searching and need some help. I am working a datalist control in ASP.NET 2.0. The datalist is connected to an SQL 2000 server and working just fine. My problem is that some of the fields in a record are blank and i want to suppress them.For example:One of my fields is Add2. No all people need a second address line so i want o supress it for them but display it for the others. Here is my code:

	 <asp:DataList 	 ID="table" 	 runat="server" 	 CellPadding="2" 	 DataKeyField="IDNumber"	 DataSourceID="MarketplaceData" 	 RepeatColumns="1" 	 GridLines="Both" 	 RepeatLayout="table"	 Width = "600px"	 ItemStyle-Font-Bold="true">				<ItemTemplate>	 <asp:Label ID="COMPANYLabel" runat="server" Text='<%# Eval("COMPANY") %>'></asp:Label><br />	 <asp:Label ID="CONTACTLabel" runat="server" Text='<%# Eval("CONTACT") %>'></asp:Label><br />	 <asp:Label ID="TITLELabel" runat="server" Text='<%# Eval("TITLE") %>'></asp:Label><br />	 <asp:Label ID="ADD1Label" runat="server" Text='<%# Eval("ADD1") %>'></asp:Label><br />	 <asp:Label ID="ADD2Label" runat="server" Text='<%#Eval("ADD2")%>'></asp:Label><br />	 <asp:Label ID="CITYLabel" runat="server" Text='<%# Eval("CITY") %>'></asp:Label>, 	 <asp:Label ID="ST_PROVLabel" runat="server" Text='<%# Eval("[ST/PROV]") %>'></asp:Label>	 <asp:Label ID="ZIPLabel" runat="server" Text='<%# Eval("ZIP") %>'></asp:Label><br />	 <asp:Label ID="COUNTRYLabel" runat="server" Text='<%# Eval("COUNTRY") %>'></asp:Label><br />	 PHONE:	 <asp:Label ID="PHONELabel" runat="server" Text='<%# Eval("PHONE") %>'></asp:Label><br />	 FAX:	 <asp:Label ID="FAXLabel" runat="server" Text='<%# Eval("FAX") %>'></asp:Label><br />	 TOLLFREE:	 <asp:Label ID="TOLLFREELabel" runat="server" Text='<%# Eval("TOLLFREE") %>'></asp:Label><br />	 <asp:HyperLink ID="HyperLink2" NavigateUrl='<%# "mailto:" & Eval("EMAIL") %>' runat="server"><%# Eval("EMAIL") %></asp:HyperLink><br />	 <asp:HyperLink ID="HyperLink1" NavigateUrl='<%# "http://" & Eval("WEBSITE") %>' Target=_blank" runat="server"><%#Eval("WEBSITE")%></asp:HyperLink><br />	 SERVICES:<asp:Label ID="Label1" runat="server" Text='<%# Eval("SERVICES") %>'></asp:Label><br />	 MemberSince:	 <asp:Label ID="MemberSinceLabel" runat="server" Text='<%# Eval("MemberSince") %>'></asp:Label><br />	 P3PARTICIPANT:	 <asp:Label ID="P3PARTICIPANTLabel" runat="server" Text='<%# Eval("P3PARTICIPANT") %>'></asp:Label><br /></ItemTemplate>	 <AlternatingItemStyle BackColor="LightSteelBlue" /></asp:DataList>

When using <%# Eval("COMPANY") %> i cant use logic like if len(Eval("ADD2")) = 0 then do something endifI am lost... can someone help me?!!!! Plz for the love of GOD! :)

Link to comment
Share on other sites

Hey Eric,You might try using an event handler to check whether there is novalue for ADD2 which then hides the Label for you. First, associate the event handler with your DataList:

<asp:DataList 	 ID="table" 	 runat="server" 	 CellPadding="2" 	 DataKeyField="IDNumber"	 DataSourceID="MarketplaceData" 	 RepeatColumns="1" 	 GridLines="Both" 	 RepeatLayout="table"	 Width = "600px"	 ItemStyle-Font-Bold="true"		 OnItemDataBound="table_ItemDataBound">

Then, in your codebehind, add something like the following:

protected void table_ItemDataBound(object sender, DataListItemEventArgs e){	DataListItem item = e.Item;	switch (item.ItemType)	{		case ListItemType.Item:		case ListItemType.AlternatingItem:			string strADD2 = DataBinder.Eval(item.DataItem, "ADD2").ToString();			if (strADD2 == null || strADD2 == String.Empty)			{				ADD2Label.Visible = false;			}			break;	}}

I hope this helps!Jesh

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...