使用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>

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

            }
        }
    }
}

这是我的webpx2的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

谢谢

纳加拉吉S

它要求您完全做到这一点。您似乎已经在DataList1上同时设置了DataSourceID和DataSource属性。删除其中之一。DataSourceID倾向于在声明性标记中设置,而DataSource倾向于在代码隐藏中设置。因为您使用的是SqlDataSource,所以请删除在代码隐藏中显式设置DataSource属性的所有行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

定位ASP:DataList中的特定控件

来自分类Dev

ASP.NET中DataLIst的ItemIndex

来自分类Dev

对asp.net C#中的不同按钮使用不同的textarea控件

来自分类Dev

c# asp.net - 在 DataList 中搜索项目并显示它们

来自分类Dev

使用.location在C#ASP.Net中添加控件

来自分类Dev

DataList ASP.net C#循环遍历

来自分类Dev

如何在DataList c# asp中动态删除行

来自分类Dev

使用C#在ASP.NET中处理颜色

来自分类Dev

使用ASP.NET C#解析JSON中的数组

来自分类Dev

使用C#在ASP.NET中处理颜色

来自分类Dev

如何从ASP.NET C#中动态生成的控件获取值?

来自分类Dev

在C#中传递Asp.Net控件的DateTime的空值

来自分类Dev

如何检测控件在ASP.NET C#中单击OnClientClick?

来自分类Dev

从ASP.net C#中的主窗体调用用户控件公共功能的问题

来自分类Dev

如何向在ASP.NET C#类中创建的控件添加属性?

来自分类Dev

需要在 TextBox 控件中获取 HttpPost 值 Unsing ASP.NET C#

来自分类Dev

使用ASP.NET C#的HTML5视频控件

来自分类Dev

如何在asp.net mvc (C#) 中使用视频控件保存视频播放的进度?

来自分类Dev

在asp.net中使用jquery更改datalist中按钮的cssclass

来自分类Dev

在ASP.NET中获取缓存的控件

来自分类Dev

ASP.NET中的RichText框控件

来自分类Dev

从C#代码隐藏错误创建ASP.NET控件

来自分类Dev

ModalPopUp作为用户控件ASP.NET C#

来自分类Dev

在ASP.NET/C#中检查复选框服务器控件时隐藏文本框-使用Jquery或Javascript

来自分类Dev

C#-ASP.NET AJAX控件的ComboBox控件在Chrome中不可编辑

来自分类Dev

使用Webforms的ASP.NET中的静态控件和动态控件有什么区别

来自分类Dev

是否可以使用jquery ui控件代替asp .net Web表单中的Web控件?

来自分类Dev

在DataList HeaderTemplate中查找控件

来自分类Dev

如何在ASP.Net C#中从父服务器端刷新UserControl或Call User控件功能?

Related 相关文章

  1. 1

    定位ASP:DataList中的特定控件

  2. 2

    ASP.NET中DataLIst的ItemIndex

  3. 3

    对asp.net C#中的不同按钮使用不同的textarea控件

  4. 4

    c# asp.net - 在 DataList 中搜索项目并显示它们

  5. 5

    使用.location在C#ASP.Net中添加控件

  6. 6

    DataList ASP.net C#循环遍历

  7. 7

    如何在DataList c# asp中动态删除行

  8. 8

    使用C#在ASP.NET中处理颜色

  9. 9

    使用ASP.NET C#解析JSON中的数组

  10. 10

    使用C#在ASP.NET中处理颜色

  11. 11

    如何从ASP.NET C#中动态生成的控件获取值?

  12. 12

    在C#中传递Asp.Net控件的DateTime的空值

  13. 13

    如何检测控件在ASP.NET C#中单击OnClientClick?

  14. 14

    从ASP.net C#中的主窗体调用用户控件公共功能的问题

  15. 15

    如何向在ASP.NET C#类中创建的控件添加属性?

  16. 16

    需要在 TextBox 控件中获取 HttpPost 值 Unsing ASP.NET C#

  17. 17

    使用ASP.NET C#的HTML5视频控件

  18. 18

    如何在asp.net mvc (C#) 中使用视频控件保存视频播放的进度?

  19. 19

    在asp.net中使用jquery更改datalist中按钮的cssclass

  20. 20

    在ASP.NET中获取缓存的控件

  21. 21

    ASP.NET中的RichText框控件

  22. 22

    从C#代码隐藏错误创建ASP.NET控件

  23. 23

    ModalPopUp作为用户控件ASP.NET C#

  24. 24

    在ASP.NET/C#中检查复选框服务器控件时隐藏文本框-使用Jquery或Javascript

  25. 25

    C#-ASP.NET AJAX控件的ComboBox控件在Chrome中不可编辑

  26. 26

    使用Webforms的ASP.NET中的静态控件和动态控件有什么区别

  27. 27

    是否可以使用jquery ui控件代替asp .net Web表单中的Web控件?

  28. 28

    在DataList HeaderTemplate中查找控件

  29. 29

    如何在ASP.Net C#中从父服务器端刷新UserControl或Call User控件功能?

热门标签

归档