WebSurfer's Home

トップ > Blog 1   |   ログイン
APMLフィルター

ListView に配置した CheckBox で選択

by WebSurfer 2012年3月17日 17:03

ユーザーが ListView 上の CheckBox にチェックを入れて複数の項目を選択後、ポストバックしてサーバー側で選択された項目を取得するという例です。

ListView に配置した CheckBox の選択結果をサーバーで取得

特に難しいことではないのですが、GridView と違って、ListView の場合はあまり例が見つからないので、忘れないように書いておきます。

ListView は「項目」(ListViewItem オブジェクト)で構成されており、そのコレクションは Items プロパティで取得できます。

後はループを回して、ListViewItem オブジェクトの中から FindControl メソッドで CheckBox オブジェクトを探し、チェックがついているかどうかを調べることができます。

そのサンプルコードは以下の通りです。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  // ListView に CheckBox を追加。ポストバックして、
  // ユーザーがチェックを入れた項目をサーバー側で取得。
    
  protected void Button1_Click(object sender, EventArgs e)
  {
    string ids = "";
    for (int i = 0; i < ListView1.Items.Count; i++ )
    {
      ListViewItem item = ListView1.Items[i];
      CheckBox cb = (CheckBox)item.FindControl("CheckBox1");
      if (cb != null)
      {
        if (cb.Checked == true)
        {
          ids += ListView1.DataKeys[i].Value.ToString() + " ";
        }
      }
    }
    Label1.Text = "Selected Products: " + ids;
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ListView と CheckBox</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:Button ID="Button1" 
      runat="server" 
      Text="PostBack" 
      OnClick="Button1_Click" />
    <asp:SqlDataSource ID="SqlDataSource1" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:Northwind %>" 
      SelectCommand=
        "SELECT [ProductID], [ProductName], [UnitPrice] 
        FROM [Products] WHERE [CategoryID]=1">
    </asp:SqlDataSource>
    <asp:ListView ID="ListView1" 
      runat="server" 
      DataKeyNames="ProductID" 
      DataSourceID="SqlDataSource1">
      <EmptyDataTemplate>
        <table id="Table1" runat="server" style="">
          <tr>
            <td>
              データは返されませんでした。</td>
          </tr>
        </table>
      </EmptyDataTemplate>

      <ItemTemplate>
        <tr style="">
          <td>
            <asp:CheckBox ID="CheckBox1" 
              runat="server" />
          </td>
          <td>
            <asp:Label ID="ProductIDLabel" 
              runat="server" 
              Text='<%# Eval("ProductID") %>' />
          </td>
          <td>
            <asp:Label ID="ProductNameLabel" 
              runat="server" 
              Text='<%# Eval("ProductName") %>' />
          </td>
          <td>
            <asp:Label ID="UnitPriceLabel" 
              runat="server" 
              Text='<%# Eval("UnitPrice") %>' />
          </td>
        </tr>
      </ItemTemplate>

      <LayoutTemplate>
        <table id="Table2" runat="server">
          <tr id="Tr1" runat="server">
            <td id="Td1" runat="server">
              <table ID="itemPlaceholderContainer" 
                runat="server" 
                border="0" 
                style="">
                <tr id="Tr2" runat="server" style="">
                  <th id="Th1" runat="server">
                    Select</th>
                  <th id="Th2" runat="server">
                    ProductID</th>
                  <th id="Th3" runat="server">
                    ProductName</th>
                  <th id="Th4" runat="server">
                    UnitPrice</th>
                </tr>
                <tr ID="itemPlaceholder" runat="server">
                </tr>
              </table>
            </td>
          </tr>
          <tr id="Tr3" runat="server">
            <td id="Td2" runat="server" style="">
            </td>
          </tr>
        </table>
      </LayoutTemplate>

    </asp:ListView>
  </div>
  <asp:Label ID="Label1" runat="server"></asp:Label>
  </form>
</body>
</html>

Tags: ,

ASP.NET

About this blog

2010年5月にこのブログを立ち上げました。主に ASP.NET Web アプリ関係の記事です。

Calendar

<<  2024年3月  >>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar