帮忙啊![NullReferenceException: 未将对象引用设置到对象的实例。]
我自己定义了一个控件,运行一直都有错误,唉,整了两天了了,找不出原因,大哥帮帮忙吧:
控件源:
<%@ 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>¥<%#DataBinder.Eval(Container.DataItem,"PPrice") %></del>
<br />
现价:¥<%#DataBinder.Eval(Container.DataItem,"PMemberPrice") %>
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
控件后台代码:
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>