2014-01-07 3 views
-2

Я пробовал пример для управления списком данных, но я получаю некоторую ошибку, которую я не могу исправить. Пожалуйста, помогите мне, исправив ошибку в моей программе.Управление DataList в ASP.NET с использованием C#

Это мой 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> 

Это мой aspx.cs файл для WebForm1:

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 для 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"]. ToString(); SqlConnection con = new SqlConnection (ConfigurationManager.ConnectionStrings ["sql connection"]. ConnectionString); SqlCommand cmd = new SqlCommand ("select * from datalist where ProductId =" + Id, con); SqlDataAdapter da = новый SqlDataAdapter (cmd); DataSet ds = new DataSet(); da.Fill (ds); какая-то проблема на странице переадресации i thinkso – Pinky

ответ

1

Это требует от вас сделать именно это. Кажется, вы установили свойства DataSourceID и DataSource в DataList1. Удалите один из них. DataSourceID имеет тенденцию быть установлен в декларативной разметке, и DataSource имеет тенденцию быть установленным в codebehind. Поскольку вы используете SqlDataSource, удалите любые строки, которые явно задают свойство DataSource в codebehind.

+0

Спасибо, я получаю вывод :) – Pinky

+0

@ Pinky приветствует .. Хороший день –

Смежные вопросы