展开菜单问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listtree.aspx.cs" Inherits="lianxi_listtree" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="select" Text='<%# Eval("typename")%>'></asp:LinkButton>
</ItemTemplate>
<SelectedItemTemplate>
<asp:DataList ID="dtl2" runat="server" RepeatColumns="2">
<ItemTemplate>
<asp:LinkButton CommandName="select2" ID="LinkButton2" runat="server" Text='<%# Eval("petname")%>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</SelectedItemTemplate>
</asp:DataList> </div>
</form>
</body>
</html>
CS文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class lianxi_listtree : datacon
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
db();
}
}
public void db()
{
this.DataList1.DataSource = bind_all("pettype");
this.DataList1.DataKeyField = "typeid";
this.DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if ( == "select")
{
string typeid = DataList1.DataKeys[e.Item.ItemIndex].ToString();
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.SelectedItem:
case ListItemType.AlternatingItem:
DataList dtl2 = (DataList)e.Item.FindControl("dtl2");
dtl2.DataSource =bind_all(typeid, "typeid", "pet");
dtl2.DataKeyField = "id";
dtl2.DataBind();
break;
}
}
}
}
提示
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 41: case ListItemType.AlternatingItem:
行 42: DataList dtl2 = (DataList)e.Item.FindControl("dtl2");
行 43: dtl2.DataSource =bind_all(typeid, "typeid", "pet");
行 44: dtl2.DataKeyField = "id";
行 45: dtl2.DataBind();
源文件: d:\site\test\controldoing\listtree.aspx.cs 行: 43