See no DataList

  • Thread starter Thread starter Thanh
  • Start date Start date
T

Thanh

I created an ASP.NET project and dropped a DataList on
the web form. I then wrote a simple class to return data:

namespace Playing
{
public class PositionData
{
private string name;
private string ticker;

public PositionData(string name, string ticker)
{
this.name = name;
this.ticker = ticker;
}

public string Name
{
get { return name; }
}

public string Ticker
{
get { return ticker; }
}
}

I then inserted the code to load data into the DataList

private void Page_Load(object sender, System.EventArgs
e)
{
if (!IsPostBack)
{
ArrayList values = new ArrayList();
values.Add(new PositionData("Microsoft", "Msft"));
values.Add(new PositionData("Intel", "Intc"));
values.Add(new PositionData("Dell", "Dell"));

DataList1.DataSource = values;
DataList1.DataBind();
}
}
}

When I run the application into IE 6, All I saw was a
blank screen with no DataList and nada. I try with
DataGrid and everything works fine.

What have I done wrong?

Thank you in advance for all your help.
 
Did you actually bind the columns to the DataList? Unlike the DataGrid, you
must visit the HTML section of the webform and bind the datafield/property
to each column in the list, whereas the datagrid will "draw" the data based
on the datafield property set in the designer.

HTH,

Morgan
 
Are these lines of code bind the DataList to its source?

DataList1.DataSource = values;
DataList1.DataBind();

According to the help files and everything I have
searched on the NET including MSDN indicates so. The code
posted in my first post was indeed coming from the sample
provided by Microsoft.

Thanh
 
Yes, that will bind the data to the datalist, but you also need the
following added to your HTML ...
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconbindingsqldatatodatalistcontro
l.htm

<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem, "price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
 
Thanks again Morgan for your help. I finally understand
what it really takes to populate the DataList and
Repeater web server controls with Data. In my opinion,
it's a very complicated and cumbersome process to have to
manually insert code into the HTML file to make it works.
It's not a user friendly way at all.

Thanh
-----Original Message-----
Yes, that will bind the data to the datalist, but you also need the
following added to your HTML ...
ms- help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconbindingsqldatat
odatalistcontro
l.htm

<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font- size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval
(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval
(Container.DataItem said:
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%
<br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %
<br>
<b>Price: </b>
<%# DataBinder.Eval
(Container.DataItem, "price", "{0:c}") %>
 
Agreed there is more work involved, but you reduce the server-side (client
side, as well) overhead by not using the DataGrid control. I've gotten into
the habit of creating a single page with the DataList &/or Repeater control
and doing a copy-paste into new pages and making changes as needed. Takes
out the redundant work (for the most part...) involved with the controls
 
Copy and paste is a good idea. By the time I gain more
knowledge about how DataList and Repeater work, I would
like to build an inherited web control to automate this
tedious process of populating the data.

Thanks again Morgan.

Merry Christmas and Happy New Year

Thanh
 
Back
Top