[英语阅读笔记]Creating Master Detail Pages using GridView

原文地址:http://www.dotnetbips.com/articles/4c3cc1c7-795e-4058-a741-e8e1de6d90a9.aspx

作者:Bipin Joshi

 

这篇文章与我之前对于Master-Detail方式的实现有所不同,而是在GridView中用GridView来显示Detail。效果如下:

535img09.jpg

文章中的实现,首先在GridView中添加两个模板列,一个用来绑定字段显示信息用,一个用来放置按钮。这个按钮里要设置CommandName属性以配合下面的一段代码。

这个按钮要实现的功能是在点击它的时候当前行展开并且在前面的绑定列中显示一个GridView来显示关联信息。

然后编辑第一个模板列,除了添加两个Label显示信息外,另加一个SqlDatasource和GridView.

实现的关键点是让里面的这个SqlDatasource接收到外面这个GridView行里传递过来的参数,依靠以下的两个代码片断:

 

private int index = 0;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

Button b = (Button)e.Row.Cells[1].FindControl("Button1");

b.CommandArgument = index.ToString();

 index=index+1;

}

}

这个代码是在外面的GridView中行被绑定的时候触发的,还有一个代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "Show")

{

Button b = (Button)GridView1.Rows[int.Parse (e.CommandArgument.ToString())].Cells[1]. FindControl("Button1");

if (b.Text == "Show")

{

string custid = GridView1.DataKeys[int.Parse (e.CommandArgument.ToString())].Value.ToString();

SqlDataSource sds = (SqlDataSource)GridView1. Rows[int.Parse(e.CommandArgument.ToString())]. FindControl("SqlDataSource2");

GridView gv = (GridView)GridView1.Rows[int.Parse (e.CommandArgument.ToString())].FindControl("GridView2");

sds.SelectParameters[0].DefaultValue = custid;

gv.Visible = true; b.Text = "Hide";

}

else

{

GridView gv = (GridView)GridView1.Rows[int.Parse (e.CommandArgument.ToString())].FindControl("GridView2");

gv.Visible = false;

b.Text = "Show";

}

}

}

 

很佩服老外的想象力,看他们的一些问题的实现总能悟到很多东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值