Repeater嵌套问题,正解后解贴
Repeater1嵌套Repeater2中,想获取Repeater2中的表[source]的cid字段值.是想根据这个值来获取用户点了Repeater2中的哪条记录,然后让它的点击次数加一,现在就是不知道怎么获取Repeater2中的表[source]的cid字段值?请高手支招,先谢了!
private void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
object cid;
//这里是获取Repeater1中的cid的值.
cid = DataBinder.Eval(e.Item.DataItem, "cid ");
Repeater Repeater2;
Repeater2 = (Repeater) e.Item.FindControl( "Repeater2 ");
string sqlnews = "select top 6 * from [source] where BigClassID= ' "+cid.ToString()+ " ' and cflag=1 order by cindex ";
DataSet ds1 = conn.GetDataSet(sqlnews, "source ");
Repeater2.DataSource = ds1.Tables[ "source "].DefaultView;
Repeater2.DataBind();
}
[解决办法]
1.
提供一个比较精简的方法, 直接设置 click 事件处理程序,
包括不嵌套情况, 以及 DataGrid GridView DataList 均可以使用次方法
2.
特别适合嵌套数据控件
3.
如果多个 button, 还是使用数据控件的 ItemCommand 方式比较合适
<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
// 首次加载数据一定要放在 !IsPostBack 内,
// 避免回发的时候再次绑定数据,覆盖复选框状态
LoadProductData();
}
}
void lnkPro_Click(object sender, EventArgs e)
{
// 触发事件的 Button
LinkButton btn = sender as LinkButton;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
Response.Write( "Selected ProductID: " + btn.CommandArgument);
Response.Write( " <br/> ");
Response.Write( "Selected RepeaterItemIndex: " + item.ItemIndex);
}
/// <summary>
/// Repeater 的每一项执行数据绑定之后调用事件处理程序,
/// 在程序内部实现绑定嵌套的 Repeater
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
void rptProCat_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// e.Item.DataItem 公开绑定到 DataList 当前项的数据,
// 这里为 包含 Category 数据的 DataRowView 对象, 因为我们提供的数据源为 DataTable
DataRowView drv = e.Item.DataItem as DataRowView;
if(drv == null) return;
// 查找嵌套的 DataList
Repeater rptPro = e.Item.FindControl( "rptPro ") as Repeater;
if (rptPro == null) return;
// 我们已经将与Category相关Product全部加载到DataSet中,
// 并建立了 DataRelation,因此这里直接反向获取 Product DataTable,
// 并使用 DataView 过滤目标数据
DataRow dr = drv.Row;
DataView dvPro = dr.Table.ChildRelations[ "ProCatShip "].ChildTable.DefaultView;
dvPro.RowFilter = "CategoryId = " + dr[ "CategoryID "].ToString();
rptPro.DataSource = dvPro;
rptPro.DataBind();
}
void LoadProductData()
{
DataSet ds = CreateProductCategoryDataSet();
rptProCat.DataSource = ds.Tables[ "Categories "];
rptProCat.DataBind();
}
#region sample data
static DataSet CreateProductCategoryDataSet()
{
DataSet ds = new DataSet( "ProductCategorySet ");
DataTable dtPro = CreateProductTable();
DataTable dtCat = CreateCategoryTable();
ds.Tables.Add(dtPro);
ds.Tables.Add(dtCat);
ds.Relations.Add( "ProCatShip ", dtCat.Columns[ "CategoryID "], dtPro.Columns[ "CategoryID "]);
return ds;
}
static DataTable CreateProductTable()
{
DataTable tbl = new DataTable( "Products ");
tbl.Columns.Add( "ProductID ", typeof(int));
tbl.Columns.Add( "ProductName ", typeof(string));
tbl.Columns.Add( "CategoryID ", typeof(int));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chai ";
row[2] = 1;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 2;
row[1] = "Chang ";
row[2] = 1;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 3;
row[1] = "Aniseed Syrup ";
row[2] = 2;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 4;
row[1] = "Chef Anton 's Cajun Seasoning ";
row[2] = 2;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 5;
row[1] = "Chef Anton 's Gumbo Mix ";
row[2] = 2;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 47;
row[1] = "Zaanse koeken ";
row[2] = 3;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 48;
row[1] = "Chocolade ";
row[2] = 3;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 49;
row[1] = "Maxilaku ";
row[2] = 3;
tbl.Rows.Add(row);
return tbl;
}
public static DataTable CreateCategoryTable()
{
DataTable tbl = new DataTable( "Categories ");
tbl.Columns.Add( "CategoryID ", typeof(int));
tbl.Columns.Add( "CategoryName ", typeof(string));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Beverages ";
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 2;
row[1] = "Condiments ";
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 3;
row[1] = "Confections ";
tbl.Rows.Add(row);
return tbl;
}
#endregion
</script>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Nested Repeater </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:Repeater ID= "rptProCat " runat= "server " OnItemDataBound= "rptProCat_ItemDataBound ">
<ItemTemplate>
<asp:Label ID= "lblCatName " runat= "server " Text= ' <%# Eval( "CategoryName ") %> '> </asp:Label>
<div style= "padding-left:20px; ">
<asp:repeater ID= "rptPro " runat= "server " >
<ItemTemplate>
<asp:linkbutton ID= "lnkPro " runat= "server " Text= ' <%# Eval( "ProductName ") %> ' CommandArgument= ' <%# Eval( "ProductID ") %> ' OnClick= "lnkPro_Click " />
<br />
</ItemTemplate>
</asp:repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Hope helpful!
[解决办法]
是想根据这个值来获取用户点了Repeater2中的哪条记录,然后让它的点击次数加一
========================================================================
你这个Repeater2控件中的数据放在什么控件了能让用户点?按钮?
[解决办法]
对。根据ID直接绑定就可以了
[解决办法]
就是点毛巾的时候是取到铅笔CID,点铅笔是铅笔CID
====================================================
楼主你的问题还是描述的不够清楚
[解决办法]
晓风残月的代码已经很全了,这种好人应该顶
LZ要么在repeater里加一个点击按钮,要么加一个隐藏列来存放CID
[解决办法]
在repeater里加一个隐藏LABEL来存放CID