Click to See Complete Forum and Search --> : [newby] ListView items don't appear under their columns


radioworld2007
September 24th, 2010, 08:43 AM
I am a newby, I am trying to display a database table with the ListView component in a ASP.NET project. The data is shown, but the 2nd column doesn't appear right under the column title...

CustomerID CompanyName
12MediaMarkt
13Lidl

I implemented it myself, without using the MSVC wizard. Anyone an idea what it is? The source is shown under...

Thanks for the help
Alex

-----


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style type="text/css">
.TableCSS
{
border-style:none;
background-color:IndianRed;
width: 700px;
}

.TableHeader
{
background-color:Crimson;
color:Snow;
font-size:large;
font-family:Verdana;
height:45px;
text-align:center;
}
.ItemCSS
{
background-color:IndianRed;
color:Snow;
font-family:MS Sans Serif;
font-size:medium;
font-weight:bold;
height:28px;
}
</style>

</head>
<body style="height: 526px">
<form id="form1" runat="server">

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="myItemPlaceHolder" DataKeyNames="CustomerID">
<ItemTemplate>
<table id="itemPlaceholderContainer" runat="server" class="ItemCSS">
<tr id="Tr2" runat="server">
<td id="Td4" runat="server">
<asp:Label ID="Td1" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>

<td id="Td5" runat="server">
<asp:Label ID="Td2" runat="server" Text='<%# Eval("CompanyName") %>' />
</td>
</tr>
</table>
</ItemTemplate>

<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" class="TableCSS" >
<tr runat="server" class="TableHeader">
<th id="Td1" runat="server">Customer ID</th>
<th id="Td2" runat="server">CompanyName</th>
<th id="Td3" runat="server">Contact Name</th>
</tr>
<tr runat="server" class="ItemCSS">
</tr>
</table>

<asp:PlaceHolder ID="myItemPlaceHolder" runat="server">
</asp:PlaceHolder>

</LayoutTemplate>
</asp:ListView>

</form>
</body>
</html>

radioworld2007
September 24th, 2010, 11:23 AM
I found out what I was doing wrong. When you are going to use a ListView without using the wizard, you need to specify manually how the data will appear. You do this with templates. If you don't specify templates, the following error message appears:

'An item placeholder must be specified on ListView 'ListView1'. Specify an item placeholder by setting a control's ID property to "itemPlaceholder". The item placeholder control must also specify runat="server".'

Below is the code that works, hardcoded in HTML. Maybe it's of use for another newbie like me :-)

Nevertheless I wonder if it's possible to generate this code from within the .cs file using C# code..

-= Alex =-



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Demo with ListView and CSS StyleSheet</title>

<style type="text/css">
.TableCSS {
border-style:none;
background-color:IndianRed;
width: 700px;
}

.TableHeaderCSS {
background-color:Crimson;
text-align:left
color:Snow;
font-size:large;
font-family:Verdana;
height:45px;
text-align:left;
}

.ItemCSS {
background-color:IndianRed;
color:Yellow;
font-family:MS Sans Serif;
font-size:medium;
font-weight:bold;
height:28px;
}
</style>
</head>

<body>
<form id="form1" runat="server">
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table ID="itemPlaceholderContainer" class="TableCSS">
<tr>
<th width="150" class="TableHeaderCSS">CustomerID</th>
<th align="left" class="TableHeaderCSS">CustomerName</th>
</tr>

<tr runat="server" ID="itemPlaceholder"/>
</table>
</LayoutTemplate>

<ItemTemplate>
<tr>
<td> <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' CssClass="ItemCSS" /> </td>
<td> <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("CompanyName") %>' CssClass="ItemCSS" /> </td>
</tr>
</ItemTemplate>
</asp:ListView>
</form>
</body>
</html>