给个DataList嵌套DataList的例子吧,求求各位大哥大姐了。
现在我只能做到子级DataList的数据绑定,但是如何激发子级里面的事件(编辑、更新和删除等)呀?真是弄不懂了。如何去找到子级DataList内部的子控件呢?求求大哥大姐...
现在我只能做到子级DataList的数据绑定,但是如何激发子级里面的事件(编辑、更新和删除等)呀?真是弄不懂了。如何去找到子级DataList内部的子控件呢?求求大哥大姐们帮一下吧,给个例子,并在思路上给个提示吧,小弟我千恩万谢了。
我在网上找不到相关资料呀,找到的都只是简单的数据绑定例子,没有事件处理的。
谢谢!谢谢了! 展开
我在网上找不到相关资料呀,找到的都只是简单的数据绑定例子,没有事件处理的。
谢谢!谢谢了! 展开
展开全部
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!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">
<!--大类-->
<asp:DataList ID="DataList1" runat="server" DataKeyField="CategoryID" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<%# Eval("CategoryName") %><hr />
<!--小类-->
<asp:DataList ID="DataList2" runat="server" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<asp:HiddenField ID="ProductID" runat="server" Value='<%# Eval("ProductID") %>' />
<%# Eval("ProductName") %>
<%# Eval("UnitPrice") %>
<asp:Button ID="Button1" runat="server" Text="显示该产品ID" CommandName="showSmall" />
</ItemTemplate>
</asp:DataList>
<br /><br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=rzzfch520"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryName], [CategoryID] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=rzzfch520"
ProviderName="System.Data.SqlClient" SelectCommand="">
</asp:SqlDataSource>
</form>
</body>
</html>
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataList1.DataSource = this.SqlDataSource1;
this.DataList1.DataBind();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
string categoryID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
this.SqlDataSource2.SelectCommand = string.Format("SELECT [ProductName], [ProductID], [UnitPrice] FROM [Products] WHERE [CategoryID]='{0}'", categoryID);
DataList dataList2 = (DataList)e.Item.FindControl("DataList2");
dataList2.DataSource = this.SqlDataSource2;
dataList2.DataBind();
}
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "showSmall")
{
HiddenField hdf = (HiddenField)e.Item.FindControl("ProductID");
ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('当前选择的产品ID是:" + hdf.Value + "');</script>");
}
}
}
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!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">
<!--大类-->
<asp:DataList ID="DataList1" runat="server" DataKeyField="CategoryID" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<%# Eval("CategoryName") %><hr />
<!--小类-->
<asp:DataList ID="DataList2" runat="server" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<asp:HiddenField ID="ProductID" runat="server" Value='<%# Eval("ProductID") %>' />
<%# Eval("ProductName") %>
<%# Eval("UnitPrice") %>
<asp:Button ID="Button1" runat="server" Text="显示该产品ID" CommandName="showSmall" />
</ItemTemplate>
</asp:DataList>
<br /><br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=rzzfch520"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryName], [CategoryID] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=rzzfch520"
ProviderName="System.Data.SqlClient" SelectCommand="">
</asp:SqlDataSource>
</form>
</body>
</html>
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataList1.DataSource = this.SqlDataSource1;
this.DataList1.DataBind();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
string categoryID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
this.SqlDataSource2.SelectCommand = string.Format("SELECT [ProductName], [ProductID], [UnitPrice] FROM [Products] WHERE [CategoryID]='{0}'", categoryID);
DataList dataList2 = (DataList)e.Item.FindControl("DataList2");
dataList2.DataSource = this.SqlDataSource2;
dataList2.DataBind();
}
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "showSmall")
{
HiddenField hdf = (HiddenField)e.Item.FindControl("ProductID");
ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('当前选择的产品ID是:" + hdf.Value + "');</script>");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询