Click DataList 中的 Button 後,如何取得 ItemTemplate 中其他控制項的內容?

本文將介紹在 Click DataList 中的 Button 後,如何取得 ItemTemplate 中其他控制項的內容?

今天在論壇上看到有網友討論到 Click DataList 中的 Button 後,如何取得 ItemTemplate 中其他控制項的內容?一般筆者都是這樣做的。首先先將 Button 的 CommandName 設定為 Select,不需要額外在 Button Click 事件中寫任何程式碼。

 

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataListDemo.aspx.cs" Inherits="Demo.DataListDemo" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title></title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:DataList ID="DataList1" runat="server" DataKeyField="RegionID" 
  13:              DataSourceID="SqlDataSource1" 
  14:              onselectedindexchanged="DataList1_SelectedIndexChanged">
  15:              <ItemTemplate>
  16:                  RegionID:
  17:                  <asp:Label ID="RegionIDLabel" runat="server" Text='<%# Eval("RegionID") %>' />
  18:                  <br />
  19:                  RegionDescription:
  20:                  <asp:Label ID="RegionDescriptionLabel" runat="server" 
  21:                      Text='<%# Eval("RegionDescription") %>' />
  22:                  <br />
  23:                  <asp:Button ID="Button1" runat="server" CommandName="select"
  24:                      Text="Button" />
  25:  <br />
  26:                  <br />
  27:              </ItemTemplate>
  28:          </asp:DataList>
  29:          <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  30:              ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
  31:              SelectCommand="SELECT [RegionID], [RegionDescription] FROM [Region]">
  32:          </asp:SqlDataSource>
  33:      </div>
  34:      </form>
  35:  </body>
  36:  </html>

 

經過上述設定,當位於 DataList 中的 Button 被 Click 後會觸發 DataList 的 SelectedIndexChanged 事件,因此我們可以在這個事件中透過 SelectedIndex 屬性來取得該項目(Item)中的任何控制項。程式碼如下:

 

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.UI;
   6:  using System.Web.UI.WebControls;
   7:   
   8:  namespace Demo
   9:  {
  10:      public partial class DataListDemo : System.Web.UI.Page
  11:      {
  12:          protected void Page_Load(object sender, EventArgs e)
  13:          {
  14:   
  15:          }        
  16:   
  17:          protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
  18:          {
  19:              string dataKey = DataList1.DataKeys[DataList1.SelectedIndex].ToString();
  20:              Label RegionIDLabel = DataList1.Items[DataList1.SelectedIndex].FindControl("RegionIDLabel") as Label;
  21:              Label RegionDescriptionLabel = DataList1.Items[DataList1.SelectedIndex].FindControl("RegionDescriptionLabel") as Label;
  22:              Response.Write(string.Format("{0},{1},{2}", dataKey, RegionIDLabel.Text, RegionDescriptionLabel.Text));
  23:          }
  24:      }
  25:  }