我自己定义了一个控件,运行一直都有错误,唉,整了两天了了,找不出原因,大哥帮帮忙吧:控件源:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="NewProduct.ascx.cs" Inherits="shop.Controls.NewProduct" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:DataList  runat="server"  RepeatColumns="3" Width="472" RepeatDirection="Horizontal" ID="Datalist1">
    <ItemTemplate>
        <table style="height:93px; width:186px; border:0px">
            <tr>
                <td style="width: 100px; height:93px;"  valign="top" >
                <a href='productDetail.aspx?ProductID=<%#DataBinder.Eval(Container.DataItem,"ProductID") %>'>
                <img height="90" alt="商品图片" src='<%#DataBinder.Eval(Container.DataItem,"PPicture") %>' style="width:85;border:0px;"/>
                </a>
                </td>
                
                <td style="width: 100px" valign="top">
                <a  title="查看详细资料" href='<%#DataBinder.Eval(Container.DataItem,"PName") %>'></a>
                <br />
                原价:<del>&yen;<%#DataBinder.Eval(Container.DataItem,"PPrice") %></del>
                <br />
                现价:&yen;<%#DataBinder.Eval(Container.DataItem,"PMemberPrice") %>
                </td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>&nbsp;

控件后台代码:
namespace shop.Controls
{
    public partial class NewProduct : System.Web.UI.UserControl
    {
       
        protected System.Web.UI.WebControls.DataList DataList1;
        protected void Page_Load(object sender, EventArgs e)
        {
            //在此处设置用户代码初始化页面
            string sSQl = "select top 3 ProductID,Pname,PPictue,PPrice,PMemberPrice from Product where PStock>0 order by ProductID desc";
            DataSet oDs = OleBase.ExecuteSql4Ds(sSQl);            DataList1.DataSource = oDs.Tables[0].DefaultView; 
            DataList1.DataBind();
           
            
        }
        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器
        /// 修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);        }
        #endregion
    }
}引用:test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<%@ Register TagPrefix="uc1" TagName="NewProduct" Src="NewProduct.ascx" %>
<!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>
        <table>
            <tr>
                <td style="width: 100px" id="TD3">
                <uc1:NewProduct ID="NewProduct" runat="server" />
                </td>
                <td style="width: 100px" id="TD2">
                </td>
                <td style="width: 100px" id="TD1">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    看看你的Product 表里,显示输出的字段有没有null值
      

  2.   

    我刚才看了一下,还是没有啊?
    我自己用控件数据源调用结果,是
    ProductID: 51
    PPrice: 4100
    PName: 西门子滚筒洗衣机TOPWM3000XS
    PPictue: pic/2003530172617.gif
    PMemberPrice: 4060 
    ProductID: 53
    PPrice: 4300
    PName: 海尔滚筒洗衣机XQG50-QF800
    PPictue: pic/2003530172938.gif
    PMemberPrice: 4250 
    ProductID: 54
    PPrice: 930
    PName: 小天鹅波轮洗衣机XQB22-21G
    PPictue: pic/2003530173128.gif
    PMemberPrice: 900 
    ProductID: 55
    PPrice: 1000
    PName: 摩托罗拉手机V998++
    PPictue: pic/2003530173338.gif
    PMemberPrice: 966 
    ProductID: 56
    PPrice: 800
    PName: 康宝消毒柜ZTP70A-4
    PPictue: pic/2003530173624.gif
    PMemberPrice: 750 
    ProductID: 57
    PPrice: 20500
    PName: 6050 F4514J
    PPictue: pic/2003530174548.jpg
    PMemberPrice: 20000
     
    应该没有空值吧!
    谢谢大哥,帮帮忙吧!唉,郁闷!
      

  3.   

    DataList1.DataSource = oDs.Tables[0].DefaultView; 
    错误在这一句,但是说什么检查null ,News之类的,
    我还是看不懂啊!
      

  4.   

    if( oDs.Tables[0]!=null )
    {
    DataList1.DataSource = oDs.Tables[0].DefaultView;}
      

  5.   

    protected System.Web.UI.WebControls.DataList DataList1;
      

  6.   

    谢谢6楼的大哥:if( oDs.Tables[0]!=null )
    {
    DataList1.DataSource = oDs.Tables[0].DefaultView;}上面的判断加了,仍旧是原来的错误。
      

  7.   

    protected System.Web.UI.WebControls.DataList DataList1;我把上面的这行改成了 DataList DataList1=new DataList()错误是没有了,但是页面什么也没有啊,但是我那个查询语句查出来的数据肯定不是空的啊,更何况我把语句改成select * from Product 页仍旧没有啊!还得麻烦大哥们有时间,抽空指教啊。唉,没有老师自学东西真难啊!
      

  8.   

    问题解决了!
    在自定义控件中已经声明了一个Datalist1;
    在后台cs中有声明了一个,DataList1,这两个中的L一个是大写,一个是小写,可我自己却给搞混了。
    所以在DataList1不加new报错,加了new页面什么都没有,原因很简单次Datalist1非彼DataList1.一句话:自己粗心,不小心。
    谢谢,谢谢,谢谢楼上的几位大哥。
    说自己遇到难题,然后网上有一帮高手,一帮大哥帮忙解决,一直是我在寝室和室友聊天引以为豪的事!