ItemDataBound为何不起作用?
.cs文件
--------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
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;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[ "connStr "]);
String cmdStr = "Select * From Pro_Type Where ParentID Is Null ";
SqlDataAdapter cmd = new SqlDataAdapter(cmdStr, conn);
DataSet ds = new DataSet();
cmd.Fill(ds, "Pro_Type ");
protype.DataSource = ds;
protype.DataBind();
conn.Close();
}
}
private void protype_ItemDataBound(object Sender, DataListItemEventArgs e)
{
Response.Write( "asdfasdfasfasdfasdfd??? ");
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList childlist = (DataList)e.Item.FindControl( "childlist ");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int ParentID = Convert.ToInt32(rowv[ "ParentID "]);
//根据分类ID查询该分类下子类,并绑定childrepeater
childlist.DataSource = GetChildsByParentId(ParentID);
childlist.DataBind();
}
}
private SqlDataReader GetChildsByParentId(int ParentID)
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[ "connStr "]);
SqlCommand cmd = new SqlCommand();
String cmdStr = "Select * From Pro_Type Where ParentID Where ParentID = @ParentID ";
cmd.CommandText = cmdStr;
cmd.Connection = conn;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
.aspx文件
----------------------------------
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
<!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 id= "Head1 " runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataList id= "protype " runat= "server ">
<ItemTemplate>
<asp:HyperLink ID= "HyperLink1 " NavigateUrl= "~/Templets/index.aspx " runat= "server "> <%# DataBinder.Eval(Container.DataItem, "TypeName ") %> </asp:HyperLink>
<!-- start child repeater -->
ads
<asp:DataList id= "childlist " runat= "server ">
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "TypeName ") %> <br/> </ItemTemplate>
</asp:DataList>
<!-- end child repeater -->
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
[解决办法]
<asp:DataList id= "protype " runat= "server ">
==========================================
<asp:DataList id= "protype " runat= "server " OnItemDataBound= "protype_ItemDataBound ">