2014-01-07 59 views
-2

我試過一些數據列表控件的例子,但我得到了一些我無法修復的錯誤。請通過糾正我程序中的錯誤來幫助我。ASP.NET中使用C的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文件:爲WebForm1的

<%@ 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> 

這是我的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()); 

      } 
     } 
    } 
} 

這我aspx.cs file for webform2:

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 

謝謝

+0

請指定你得到什麼錯誤? –

+0

和什麼是錯誤?也解釋你是否試圖做 –

+0

其實我在下面的部分,字符串Id = Request [「Id」]。 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);重定向頁面中的一些問題,我認爲 – Pinky

回答

1

它要求你這樣做。您好像在DataList1上設置了DataSourceID和DataSource屬性。刪除其中一個。 DataSourceID傾向於在聲明性標記中設置,而DataSource傾向於在代碼隱藏中設置。因爲您使用的是SqlDataSource,所以刪除所有在代碼隱藏中顯式設置DataSource屬性的行。

+0

謝謝你我輸出:) – Pinky

+0

@Pinky歡呼..祝你有個美好的一天 –