抓取大型控制項中的子控制項方法有兩種:
1. .FindControl() 方法
2. .Controls[] 集合
.FindControl() 方法: (重點)
1.首先將要抓取的子控制項轉成樣板
2.注意要抓取的子控制項的id (例如:這個範例要抓取子控制項中放的是Label,就要注意此Label的id)(P10-18)
3.用.FindControl()抓取
詳細情形可見: P10-16
後置程式碼如下:
protected void Page_Load(object sender, EventArgs e)
{
//若是第一次執行,就抓取GV第一列的title欄位值
if (!Page.IsPostBack)
{
Label LB = (Label)GridView1.Rows[0].FindControl("Label1");
Response.Write(LB.Text);
}
}
//如果觸發選取事件
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//抓取被選取那一列的title值
Label LB = (Label)GridView1.Rows[e.NewSelectedIndex].FindControl("Label1");
Response.Write(LB.Text);
}
注意:
會抓到title的值是因為,title顯示樣板中是用Lable顯示,而我們抓的又是這個Label的id