asp.net Repeater顯示父子表數(shù)據(jù),無閃爍
更新時間:2009年12月10日 22:24:28 作者:
兩天在改項目bug,發(fā)現(xiàn)以前有人做的repeater顯示父子表結(jié)構(gòu)展開和關(guān)閉子表數(shù)據(jù)時總是有閃爍,于是就試著改成無閃爍的,成功了,與大家分享.
效果圖如下:
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;
namespace RepeaterTest
{
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bind();
}
}
private DataSet dataSource
{
get
{
if (ViewState["ds"] != null)
{
return (DataSet)ViewState["ds"];
}
else
{
DataSet ds = DataSource.GetDataSet();
ViewState["ds"] = ds;
return ds;
}
}
}
private void bind()
{
DataSet ds = this.dataSource;
this.Repeater1.DataSource = ds.Tables["tbclass"];
this.Repeater1.DataBind();
this.Repeater2.DataSource = ds.Tables["tbclass"];
this.Repeater2.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "expand")
{
LinkButton btn = (LinkButton)e.CommandSource;
GridView grid =(GridView)btn.Parent.FindControl("grid1");
if (btn.Text == "+")
{
btn.Text = "-";
grid.Visible = true;
}
else
{
btn.Text = "+";
grid.Visible = false;
}
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.dataSource.Tables["tbstu"];
LinkButton objButton = (LinkButton)e.Item.FindControl("btnExpand");
if (objButton != null && objButton.CommandArgument !="")
{
string classID = objButton.CommandArgument;
dt.DefaultView.RowFilter = "ClassID=" + classID;
DataTable dtStu = dt.DefaultView.ToTable();
GridView grid = (GridView)e.Item.FindControl("grid1");
grid.DataSource = dtStu;
grid.DataBind();
}
}
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.dataSource.Tables["tbstu"];
Label objlbl = (Label)e.Item.FindControl("lblclassid");
if (objlbl != null && objlbl.Text != "")
{
string classID = objlbl.Text;
dt.DefaultView.RowFilter = "ClassID=" + classID;
DataTable dtStu = dt.DefaultView.ToTable();
GridView grid = (GridView)e.Item.FindControl("grid2");
grid.DataSource = dtStu;
grid.DataBind();
}
}
}
}
文件打包下載RepeaterTest.rar
有用得著的同志可以參考..
源代碼如下:
復(fù)制代碼 代碼如下:
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;
namespace RepeaterTest
{
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bind();
}
}
private DataSet dataSource
{
get
{
if (ViewState["ds"] != null)
{
return (DataSet)ViewState["ds"];
}
else
{
DataSet ds = DataSource.GetDataSet();
ViewState["ds"] = ds;
return ds;
}
}
}
private void bind()
{
DataSet ds = this.dataSource;
this.Repeater1.DataSource = ds.Tables["tbclass"];
this.Repeater1.DataBind();
this.Repeater2.DataSource = ds.Tables["tbclass"];
this.Repeater2.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "expand")
{
LinkButton btn = (LinkButton)e.CommandSource;
GridView grid =(GridView)btn.Parent.FindControl("grid1");
if (btn.Text == "+")
{
btn.Text = "-";
grid.Visible = true;
}
else
{
btn.Text = "+";
grid.Visible = false;
}
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.dataSource.Tables["tbstu"];
LinkButton objButton = (LinkButton)e.Item.FindControl("btnExpand");
if (objButton != null && objButton.CommandArgument !="")
{
string classID = objButton.CommandArgument;
dt.DefaultView.RowFilter = "ClassID=" + classID;
DataTable dtStu = dt.DefaultView.ToTable();
GridView grid = (GridView)e.Item.FindControl("grid1");
grid.DataSource = dtStu;
grid.DataBind();
}
}
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.dataSource.Tables["tbstu"];
Label objlbl = (Label)e.Item.FindControl("lblclassid");
if (objlbl != null && objlbl.Text != "")
{
string classID = objlbl.Text;
dt.DefaultView.RowFilter = "ClassID=" + classID;
DataTable dtStu = dt.DefaultView.ToTable();
GridView grid = (GridView)e.Item.FindControl("grid2");
grid.DataSource = dtStu;
grid.DataBind();
}
}
}
}
文件打包下載RepeaterTest.rar
有用得著的同志可以參考..
您可能感興趣的文章:
相關(guān)文章
asp.net使用WebAPI和EF框架結(jié)合實現(xiàn)數(shù)據(jù)的基本操作
這篇文章介紹了asp.net使用WebAPI和EF框架結(jié)合實現(xiàn)數(shù)據(jù)基本操作的案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
ASP.NET設(shè)計網(wǎng)絡(luò)硬盤之上傳文件實現(xiàn)代碼
用戶最終是要和文件打交道的,文件夾僅僅是用來方便管理的。文件的上傳和下載也就成為“網(wǎng)絡(luò)硬盤”功能設(shè)計中的重要一環(huán)2012-10-10
asp.net 數(shù)據(jù)庫連接類代碼(SQL)
asp.net數(shù)據(jù)庫連接類(SQL) 代碼,需要的朋友可以參考下。2010-03-03

