这个datalist问题困扰我很多天了,希望在这里有人能够帮我解决,数据库用的是sqlserver中自带pubs
WebForm1.aspx的代码:<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="datalist_test.WebForm1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:datalist id="DataList1" GridLines="Horizontal" CellPadding="4" BackColor="White" BorderColor="#E3E3E3"
DataKeyField="ProductID" BorderWidth="1px" Width="100%" runat="server" HeaderStyle-BackColor="#E3E3E3"
HeaderStyle-BorderColor="Peachpuff" Height="34">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#339966"></SelectedItemStyle>
<HeaderTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD width="10"></TD>
<TD><b>标识ID</b></TD>
</TR>
</TABLE>
</HeaderTemplate>
<AlternatingItemStyle BackColor="WhiteSmoke"></AlternatingItemStyle> <ItemStyle ForeColor="#333333" BorderStyle="None" BackColor="White"></ItemStyle>
<ItemTemplate>
<table align="center" border="0" cellpadding="0" cellspacing="1" width="100%">
<tr>
<td width="10"><a href='<%# "AddToCart.aspx?title=" + DataBinder.Eval(Container.DataItem, "title_id") %>' target=A></a></td>
</tr>
</table>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" ForeColor="#333333" BackColor="#E3E3E3"></FooterStyle>
<HeaderStyle Font-Bold="True" Height="26px" ForeColor="White"></HeaderStyle>
</asp:datalist>
<br>
<asp:datalist id="Datalist2" GridLines="Both" CellPadding="4" BackColor="White" BorderColor="#E3E3E3"
DataKeyField="ProductID" BorderWidth="1px" Width="100%" runat="server" HeaderStyle-BackColor="#E3E3E3">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<HeaderTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD width="10"></TD>
<TD><b>标识ID</b></TD>
</TR>
</TABLE>
</HeaderTemplate>
<AlternatingItemStyle BackColor="WhiteSmoke"></AlternatingItemStyle>

<ItemStyle ForeColor="#003399" BorderStyle="None" BackColor="White"></ItemStyle>
<ItemTemplate>
<table align="center" border="0" cellpadding="0" cellspacing="1" width="100%">
<tr>
<td width="10"><a href='<%# "Cart.aspx?ID=" + DataBinder.Eval(Container.DataItem, "pub_id") %>' target=A><img src="Images/ShoppingCart.gif" border="0" title="点击购买"></a></td>
<td width="20"><%# DataBinder.Eval(Container.DataItem,"notes")%></td>
</tr>
</table>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" ForeColor="#003399" BackColor="#E3E3E3"></FooterStyle>
<HeaderStyle Font-Bold="True" Height="26px" ForeColor="#CCCCFF"></HeaderStyle>
</asp:datalist>
WebForm1.aspx.cs:数据库的设置代码如下:
SqlConnection myConnection = new SqlConnection("Server=localhost;uid=pub_land;pwd=comeon;database=pubs");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection); }我想在页面实现两datalist的数据绑定,Webform1.aspx的代码就是那样的,的WebForm.aspx.cs的代码怎么写啊

解决方案 »

  1.   

    这有什么难么????试试(没测试,也许会有错)
    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;
    using System.Data.SqlClient;namespace DataAccess
    {
    public class Pubs
    {
    string strConn;
    public Pubs (string strConn)
    {
    this.strConn = strConn;
    } public DataTable GetTitles()
    {
    using(SqlConnection myConnection = new SqlConnection(strConn))
    {
    SqlDataAdapter myAdapter = new SqlDataAdapter("select * from Titles", myConnection); DataTable dt = new DataTable();
    myAdapter.Fill(dt);
    return dt;
    } return null;
    }
    }
    }namespace WebApplication1
    {
    public class WebForm1 : System.Web.UI.Page
    { protected System.Web.UI.WebControls.DataList DataList1;
    protected System.Web.UI.WebControls.DataList DataList2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    DataAccess.Pubs d = new DataAccess.Pubs("Server=localhost;uid=pub_land;pwd=comeon;database=pubs");//也许应该从配置文件里获取
    DataTable dt = d.GetTitles();

    DataList1.DataSource = dt;
    DataList1.DataBind(); DataList2.DataSource = dt;
    DataList2.DataBind();

    }
    } override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);  
    }
    }
    }
      

  2.   

    出什么错?贴出来,确认你的命名空间和类名跟你webform1.aspx里的一样
    namespace datalist_test
    {
    public class WebForm1 : System.Web.UI.Page
    ...
      

  3.   

    我所用的WebForm1.aspx和WebForm.aspx.cs和上面写的一模一样,下面是编译结果:编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0117: “ASP.WebForm1_aspx”并不包含对“Datalist2”的定义源错误:行 562:            #line default
    行 563:            #line hidden
    行 564:            this.Datalist2 = __ctrl;
    行 565:            
    行 566:            #line 28 "http://localhost/datalist6/WebForm1.aspx"
     源文件: c:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\datalist6\afc063b5\5179378f\yyook79z.0.cs    行: 564 
      

  4.   

    protected System.Web.UI.WebControls.DataList DataList2;
    <asp:datalist id="DataList2" GridLines="Both" CellPadding="4" BackColor="White" BorderColor="#E3E3E3" runat="server"