datalist repeating first record

  • Thread starter Thread starter Lewis
  • Start date Start date
L

Lewis

Hey!

Wondering if anyone can help!

I have been trying to build an aspx page which builds a list of records from
a database. Having been doing this using xml and xslt using asp for years
with no problems this particular page had some functionality which I thought
could be well suited to a data list control.
Maybe I'm misunderstanding how this control is supposed to work but it
doesn't do what I'm expecting!

Here's what I did: (briefly)

* added the connection, adapter and dataset (creating the xsd as well) to my
aspx page.
* previewed the dataset (all OK)
* added the data list to the page.
* edited the 'tem' template - and added a literal control.
* set the datalists's datasource to be my dataset
* bound the literal control to a column in my dataset
* added the code to fill the dataset and bind everything on the page to its
databindings

The page compiled OK and when viewed it had 16 literal controls (the same
amount of records as is in my table) : but all controls had the value from
the first row of my table.
After trying to figure this out I decided to check that the number of
records wasn't just a coincidence so I removed all but two records - the
page then showed just two literal controls!
I have successfully got this working with a datagrid but a datalist suits my
needs more appropriately. Am I misunderstanding how the datalist control is
supposed to work?

Wish Id used XML / XSLT to start off with now. : (

Thanks in advance for the help!

Lewis
 
code follows

(pretty much all generated by VS.net )


ASPX: -->

<%@ Page language="c#" Codebehind="imagesDL.aspx.cs" AutoEventWireup="false"
Inherits="namespace.subnamespace.imagesDL" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>imagesDL</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="imagesDL" method="post" runat="server">
<asp:DataList id=DataList1 style="Z-INDEX: 101; LEFT: 2px; POSITION:
absolute; TOP: 12px" runat="server" DataSource="<%# oDSImages1 %>"
DataMember="IMAGE" DataKeyField="image_id">
<ItemTemplate>
<asp:Literal id=Literal1 runat="server" Text='<%#
DataBinder.Eval(oDSImages1, "Tables[IMAGE].DefaultView.[0].caption") %>'>
</asp:Literal>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>

code behind : --->
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 namespace.subnamespace
{
/// <summary>
/// Summary description for imagesDL.
/// </summary>
public class imagesDL : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected WelshHeroes._admin.oDSImages oDSImages1;
protected System.Web.UI.WebControls.DataList DataList1;

private void Page_Load(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(oDSImages1);
Page.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.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.oDSImages1 = new WelshHeroes._admin.oDSImages();

((System.ComponentModel.ISupportInitialize)(this.oDSImages1)).BeginInit();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table",
"IMAGE", new System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("image_id", "image_id"),
new
System.Data.Common.DataColumnMapping("name", "name"),
new
System.Data.Common.DataColumnMapping("caption", "caption"),
new
System.Data.Common.DataColumnMapping("location", "location"),
new
System.Data.Common.DataColumnMapping("thumb_location", "thumb_location"),
new
System.Data.Common.DataColumnMapping("location_local", "location_local"),
new
System.Data.Common.DataColumnMapping("thumb_location_local",
"thumb_location_local"),
new
System.Data.Common.DataColumnMapping("filename", "filename"),
new
System.Data.Common.DataColumnMapping("thumb_filename", "thumb_filename"),
new
System.Data.Common.DataColumnMapping("isLive", "isLive")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT image_id, name, caption,
location, thumb_location, location_local, thumb_l" +
"ocation_local, filename, thumb_filename, isLive FROM IMAGE";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "connstring";
//
// oDSImages1
//
this.oDSImages1.DataSetName = "oDSImages";
this.oDSImages1.Locale = new System.Globalization.CultureInfo("en-GB");
this.oDSImages1.Namespace = "http://www.tempuri.org/oDSImages.xsd";
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.oDSImages1)).EndInit();

}
#endregion
}
}
 
Hi Lewis,

Try changing this bind expression from :

<ItemTemplate>
<asp:Literal id=Literal1 runat="server"
Text='<%#DataBinder.Eval(oDSImages1,
"Tables[IMAGE].DefaultView.[0].caption") %>'>
</asp:Literal>
</ItemTemplate>

to:

<ItemTemplate>
<span><%#DataBinder.Eval( Container.DataItem, "caption") %></span>
</asp:Literal>
</ItemTemplate>


I change the literal control to <Span> cause the latter does not generate
overload on the server side.
beside that I think that the problem is in the first parameter of Eval ( )
it should be Container.DataItem

Try it and let me know,

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Lewis said:
code follows

(pretty much all generated by VS.net )


ASPX: -->

<%@ Page language="c#" Codebehind="imagesDL.aspx.cs" AutoEventWireup="false"
Inherits="namespace.subnamespace.imagesDL" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>imagesDL</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="imagesDL" method="post" runat="server">
<asp:DataList id=DataList1 style="Z-INDEX: 101; LEFT: 2px; POSITION:
absolute; TOP: 12px" runat="server" DataSource="<%# oDSImages1 %>"
DataMember="IMAGE" DataKeyField="image_id">
<ItemTemplate>
<asp:Literal id=Literal1 runat="server" Text='<%#
DataBinder.Eval(oDSImages1, "Tables[IMAGE].DefaultView.[0].caption") %>'>
</asp:Literal>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>

code behind : --->
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 namespace.subnamespace
{
/// <summary>
/// Summary description for imagesDL.
/// </summary>
public class imagesDL : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected WelshHeroes._admin.oDSImages oDSImages1;
protected System.Web.UI.WebControls.DataList DataList1;

private void Page_Load(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(oDSImages1);
Page.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.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.oDSImages1 = new WelshHeroes._admin.oDSImages();

((System.ComponentModel.ISupportInitialize)(this.oDSImages1)).BeginInit();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table",
"IMAGE", new System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("image_id", "image_id"),
new
System.Data.Common.DataColumnMapping("name", "name"),
new
System.Data.Common.DataColumnMapping("caption", "caption"),
new
System.Data.Common.DataColumnMapping("location", "location"),
new
System.Data.Common.DataColumnMapping("thumb_location", "thumb_location"),
new
System.Data.Common.DataColumnMapping("location_local", "location_local"),
new
System.Data.Common.DataColumnMapping("thumb_location_local",
"thumb_location_local"),
new
System.Data.Common.DataColumnMapping("filename", "filename"),
new
System.Data.Common.DataColumnMapping("thumb_filename", "thumb_filename"),
new
System.Data.Common.DataColumnMapping("isLive", "isLive")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT image_id, name, caption,
location, thumb_location, location_local, thumb_l" +
"ocation_local, filename, thumb_filename, isLive FROM IMAGE";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "connstring";
//
// oDSImages1
//
this.oDSImages1.DataSetName = "oDSImages";
this.oDSImages1.Locale = new System.Globalization.CultureInfo("en-GB");
this.oDSImages1.Namespace = "http://www.tempuri.org/oDSImages.xsd";
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.oDSImages1)).EndInit();

}
#endregion
}
}
 
Back
Top