使用C#在ASP.NET中进行DataList控件

时间:2014-01-07 07:16:16

标签: c# asp.net datalist

我已经尝试了一些数据列表控制的例子但是我得到了一些我无法解决的错误。请通过纠正我的程序中的错误来帮助我。

这是我的aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datalist.WebForm1" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" 
            OnItemCommand="DataList1_ItemCommand" Height="774px">
        <ItemTemplate> <asp:Panel ID="Panel1" runat="server" BackColor="#FF9933" BorderWidth="3px" Height="380px" Width="270px">
        <table>
        <tr >
        <td width="75%" style="color: #0000FF; font-weight: bold">
        <asp:Label ID="lbl" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label></td></tr>
        <tr >
        <td width="50%" style="color: #009900; font-weight: bold">
        <span style="color: Black; font-weight: bold;">ProductDetails:</span><br />
        <asp:Label ID="lbl2" runat="server" Text='<%#Eval("ProductDescription") %>'></asp:Label>
        </td>
        </tr>
        <tr >
        <td width="75%" style="color: #FF0000; font-weight: bold"><span style="color: Black; font-weight: bold;">Price:</span>
        <br /><asp:Label ID="lbl3" runat="server" Text='<%#Eval("ProductCost") %>'></asp:Label>
        </td>
        </tr>
        <tr>
        <td align="right">
        <asp:LinkButton ID="LinkButton1" runat="server"
        Font-Underline="False" style="font-weight: 700; color: Black" CommandName="ViewDetails" CommandArgument='<%#Eval("ProductId") %>' BackColor="#FF9933">ViewDeatils</asp:LinkButton>
        </td></tr>
                </table>
                </asp:Panel>
        </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

这是我的另一个aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="datalist.WebForm2" %>

<!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">
.style1
{
width: 100%;
}
.style2
{
width: 369px;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <table class="style1">
        <tr>
        <td style="color: #0000FF; font-weight: 700" >
        <span style="color: Black; font-weight: bold;">Modal:</span><br /><asp:Literal ID="Literal1" runat="server"></asp:Literal>
        </td>
        </tr>
        <tr>
        <td style="font-weight: 700; color: #009933" >
        <span style="color: Black; font-weight: bold;">ProductDetails:</span><br /><asp:Literal ID="Literal2" runat="server"></asp:Literal>
        </td>
        </tr>
        <tr>
        <td style="font-weight: 700; color: #FF0000" >
        <span style="color: Black; font-weight: bold;">Price:</span><br /><asp:Literal ID="Literal3" runat="server"></asp:Literal>
        </td>
        </tr>
        </table>

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

这是我的webform1的aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace datalist
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetData();
        }

        public void GetData()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql connection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from datalist", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataList1.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataBind();

        }

        protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
        {
            if (e.CommandName == "ViewDetails")
            {
                Response.Redirect("WebForm2.aspx?Id=" + e.CommandArgument.ToString());

            }
        }
    }
}

这是我的webform2的aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace datalist
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetData();
        }

        public void GetData()
        {
            string Id = Request["Id"].ToString();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql connection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from datalist where ProductId = " + Id, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Literal1.Text = ds.Tables[0].Rows[0][1].ToString();
            Literal2.Text = ds.Tables[0].Rows[0][2].ToString();
            Literal3.Text = ds.Tables[0].Rows[0][3].ToString();

        }
    }
}

我收到这样的错误

Server Error in '/' Application.
Both DataSource and DataSourceID are defined on 'DataList1'.  Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'DataList1'.  Remove one definition.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Both DataSource and DataSourceID are defined on 'DataList1'.  Remove one definition.]
   System.Web.UI.WebControls.BaseDataList.ConnectToDataSourceView() +8685854
   System.Web.UI.WebControls.BaseDataList.OnLoad(EventArgs e) +19
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Control.LoadRecursive() +146
   System.Web.UI.Control.LoadRecursive() +146
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

谢谢

1 个答案:

答案 0 :(得分:2)

它要求你做到这一点。您似乎已在DataList1上设置了DataSourceID和DataSource属性。删除其中一个。 DataSourceID倾向于在声明性标记中设置,并且DataSource倾向于在代码隐藏中设置。因为您正在使用SqlDataSource,所以删除在代码隐藏中显式设置DataSource属性的所有行。