DataListCommandEventArgsとは? わかりやすく解説

DataListCommandEventArgs クラス

DataList コントロールの CancelCommand、DeleteCommand、EditCommand、ItemCommand、UpdateCommand の各イベントデータ提供します。このクラス継承できません。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Class DataListCommandEventArgs
    Inherits CommandEventArgs
Dim instance As DataListCommandEventArgs
public class DataListCommandEventArgs : CommandEventArgs
public ref class DataListCommandEventArgs :
 public CommandEventArgs
public class DataListCommandEventArgs extends
 CommandEventArgs
public class DataListCommandEventArgs extends
 CommandEventArgs
解説解説
使用例使用例

DataList コントロールEditCommandUpdateCommandCancelCommand、および DeleteCommand の各イベントイベント ハンドラ定義する方法次の例に示します。この例では、DataList コントロールの各コマンド プロパティイベント ハンドラ設定し宣言によってイベント ハンドラ指定します

<%@ Page Language="VB" AutoEventWireup="True"
 %>
<%@ Import Namespace="System.Data"
 %>

<html>
<head>

   <script runat="server">

      ' The Cart and CartView objects temporarily store the data source
      ' for the DataList control while the page is being processed.
      Dim Cart As DataTable = New
 DataTable()
      Dim CartView As DataView
 
      Sub Page_Load(sende As Object,
 e As EventArgs) 
 
         ' With a database, use an select query to retrieve the data.
         ' Because the data source in this example is an in-memory
         ' DataTable, retrieve the data from session state if it exists;
         ' otherwise, create the data source.
         GetSource()

         ' The DataGrid control maintains state between posts to the
 server;
         ' it only needs to be bound to a data source the first time
 the
         ' page is loaded or when the data source is updated.
         If Not IsPostBack Then

            BindList()

         End If
                   
      End Sub

      Sub BindList() 

         ' Set the data source and bind to the DataList control.
         ItemsList.DataSource = CartView
         ItemsList.DataBind()

      End Sub

      Sub GetSource()

         ' For this example, the data source is a DataTable that
         ' is stored in session state. If the data source does not exist
,
         ' create it; otherwise, load the data.
         If Session("ShoppingCart")
 Is Nothing Then 

            ' Create the sample data.
            Dim dr As DataRow  
 
            ' Define the columns of the table.
            Cart.Columns.Add(new DataColumn("Qty",
 GetType(Int32)))
            Cart.Columns.Add(new DataColumn("Item",
 GetType(String)))
            Cart.Columns.Add(new DataColumn("Price",
 GetType(Double)))

            ' Store the table in session state to persist its values
            ' between posts to the server.
            Session("ShoppingCart") = Cart
             
            ' Populate the DataTable with sample data.
            Dim i As Integer

            For i = 1 To 9 
            
               dr = Cart.NewRow()
               If (i Mod 2) <> 0 Then

                  dr(0) = 2
               
               Else
               
                  dr(0) = 1
               
               End If

               dr(1) = "Item " & i.ToString()
               dr(2) = (1.23 * (i + 1))
               Cart.Rows.Add(dr)
            
            Next i

         Else

            ' Retrieve the sample data from session state.
            Cart = CType(Session("ShoppingCart"),
 DataTable)

         End If         
 
         ' Create a DataView and specify the field to sort by.
         CartView = New DataView(Cart)
         CartView.Sort="Item"

         Return

      End Sub

      Sub Edit_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Set the EditItemIndex property to the index of the item clicked
         ' in the DataList control to enable editing for that item.
 Be sure
         ' to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = e.Item.ItemIndex
         BindList()

      End Sub

      Sub Cancel_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Set the EditItemIndex property to -1 to exit editing mode.
 Be sure
         ' to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1
         BindList()

      End Sub

      Sub Delete_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Retrieve the name of the item to remove.
         Dim item As String
 = (CType(e.Item.FindControl("ItemLabel"), Label)).Text

         ' Filter the CartView for the selected item and remove it from
         ' the data source.
         CartView.RowFilter = "Item='" & item & "'"
         If CartView.Count > 0 Then 
       
            CartView.Delete(0)
         
         End If
         CartView.RowFilter = ""

         ' Set the EditItemIndex property to -1 to exit editing mode.
 Be sure 
         ' to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1
         BindList()

      End Sub

      Sub Update_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Retrieve the updated values from the selected item.
         Dim item As String
 = _
             (CType(e.Item.FindControl("ItemLabel"),
 Label)).Text
         Dim qty As String
 = _
             (CType(e.Item.FindControl("QtyTextBox"),
 TextBox)).Text
         Dim price As String
 = _
             (CType(e.Item.FindControl("PriceTextBox"),
 TextBox)).Text

         ' With a database, use an update command to update the data.
         ' Because the data source in this example is an in-memory 
         ' DataTable, delete the old row and replace it with a new one.

         ' Filter the CartView for the selected item and remove it from
 
         ' the data source.
         CartView.RowFilter = "Item='" & item & "'"
         If CartView.Count > 0 Then 
       
            CartView.Delete(0)
         
         End If
         CartView.RowFilter = ""

         ' ***************************************************************
         ' Insert data validation code here. Make sure to validate the
         ' values entered by the user before converting to the appropriate
         ' data types and updating the data source.
         ' ***************************************************************

         ' Add a new entry to replace the previous item.
         Dim dr As DataRow = Cart.NewRow()
         dr(0) = qty
         dr(1) = item
         ' If necessary, remove the '$' character from the price before
 
         ' converting the price to a Double.
         If price.Chars(0) = "$"
 Then

            dr(2) = Convert.ToDouble(price.Substring(1))

         Else

            dr(2) = Convert.ToDouble(price)
         
         End If

         Cart.Rows.Add(dr)

         ' Set the EditItemIndex property to -1 to exit editing mode.
 
         ' Be sure to rebind the DataList to the data source to refresh
 
         ' the control.
         ItemsList.EditItemIndex = -1
         BindList()

      End Sub

   </script>

</head>
<body>

   <form runat=server>

      <h3>DataList Edit Example</h3>

      Click <b>Edit</b> to edit the values of
 the item.

      <br><br>
       
      <asp:DataList id="ItemsList"
           GridLines="Both"
           RepeatColumns="3"
           RepeatDirection="Horizontal"
           CellPadding="3"
           CellSpacing="0"
           OnEditCommand="Edit_Command"
           OnUpdateCommand="Update_Command"
           OnDeleteCommand="Delete_Command"
           OnCancelCommand="Cancel_Command"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <EditItemStyle BackColor="yellow">
         </EditItemStyle>

         <HeaderTemplate>

            Items

         </HeaderTemplate>
         
         <ItemTemplate>

            Item:
            <%# DataBinder.Eval(Container.DataItem, "Item")
 %> 

            <br>

            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "Qty")
 %>

            <br>

            Price:
            <%# DataBinder.Eval(Container.DataItem, "Price",
 "{0:c}") %>

            <br>

            <asp:LinkButton id="EditButton" 
                 Text="Edit" 
                 CommandName="Edit"
                 runat="server"/>

         </ItemTemplate>
              
         <EditItemTemplate>

            Item:
            <asp:Label id="ItemLabel" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Item")
 %>' 
                 runat="server"/>

            <br>

            Quantity:
            <asp:TextBox id="QtyTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Qty")
 %>' 
                 runat="server"/>

            <br>

            Price:
            <asp:TextBox id="PriceTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Price",
 "{0:c}") %>' 
                 runat="server"/>

            <br>

            <asp:LinkButton id="UpdateButton" 
                 Text="Update" 
                 CommandName="Update" 
                 runat="server"/>

            <asp:LinkButton id="DeleteButton" 
                 Text="Delete" 
                 CommandName="Delete" 
                 runat="server"/>

            <asp:LinkButton id="CancelButton" 
                 Text="Cancel" 
                 CommandName="Cancel" 
                 runat="server"/>

         </EditItemTemplate>

      </asp:DataList>

   </form>

</body>
</html>

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>
<head>

   <script runat="server">

      // The Cart and CartView objects temporarily store the data source
      // for the DataList control while the page is being processed.
      DataTable Cart = new DataTable();
      DataView CartView;   
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         // With a database, use an select query to retrieve the data.
 
         // Because the data source in this example is an in-memory
         // DataTable, retrieve the data from session state if it exists;
         // otherwise, create the data source.
         GetSource();

         // The DataList control maintains state between posts to the
 server;
         // it only needs to be bound to a data source the first time
 the
         // page is loaded or when the data source is updated.
         if (!IsPostBack)
         {

            BindList();

         }
                   
      }

      void BindList() 
      {

         // Set the data source and bind to the DataList control.
         ItemsList.DataSource = CartView;
         ItemsList.DataBind();

      }

      void GetSource()
      {

         // For this example, the data source is a DataTable that 
         // is stored in session state. If the data source does not
 exist,
         // create it; otherwise, load the data.
         if (Session["ShoppingCart"] == null)
 
         {     

            // Create the sample data.
            DataRow dr;  
 
            // Define the columns of the table.
            Cart.Columns.Add(new DataColumn("Qty", typeof(Int32)));
            Cart.Columns.Add(new DataColumn("Item",
 typeof(String)));
            Cart.Columns.Add(new DataColumn("Price",
 typeof(Double)));

            // Store the table in session state to persist its values
 
            // between posts to the server.
            Session["ShoppingCart"] = Cart;
             
            // Populate the DataTable with sample data.
            for (int i = 1; i <= 9; i++)
 
            {
               dr = Cart.NewRow();
               if (i % 2 != 0)
               {
                  dr[0] = 2;
               }
               else
               {
                  dr[0] = 1;
               }
               dr[1] = "Item " + i.ToString();
               dr[2] = (1.23 * (i + 1));
               Cart.Rows.Add(dr);
            }

         } 

         else
         {

            // Retrieve the sample data from session state.
            Cart = (DataTable)Session["ShoppingCart"];

         }         
 
         // Create a DataView and specify the field to sort by.
         CartView = new DataView(Cart);
         CartView.Sort="Item";

         return;

      }

      void Edit_Command(Object sender, DataListCommandEventArgs
 e) 
      {

         // Set the EditItemIndex property to the index of the item
 clicked
         // in the DataList control to enable editing for that item.
 Be sure
         // to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = e.Item.ItemIndex;
         BindList();

      }

      void Cancel_Command(Object sender, DataListCommandEventArgs
 e) 
      {

         // Set the EditItemIndex property to -1 to exit editing mode.
 Be sure
         // to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1;
         BindList();

      }

      void Delete_Command(Object sender, DataListCommandEventArgs
 e) 
      { 

         // Retrieve the name of the item to remove.
         String item = ((Label)e.Item.FindControl("ItemLabel")).Text;

         // Filter the CartView for the selected item and remove it
 from
         // the data source.
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0) 
         {
            CartView.Delete(0);
         }
         CartView.RowFilter = "";

         // Set the EditItemIndex property to -1 to exit editing mode.
 Be sure
         // to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1;
         BindList();

      }

      void Update_Command(Object sender, DataListCommandEventArgs
 e) 
      {

         // Retrieve the updated values from the selected item.
         String item = ((Label)e.Item.FindControl("ItemLabel")).Text;
         String qty = ((TextBox)e.Item.FindControl("QtyTextBox")).Text;
         String price = ((TextBox)e.Item.FindControl("PriceTextBox")).Text;

         // With a database, use an update command to update the data.
         // Because the data source in this example is an in-memory
 
         // DataTable, delete the old row and replace it with a new
 one.

         // Filter the CartView for the selected item and remove it
 from
         // the data source.
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0)
         {
            CartView.Delete(0);
         }
         CartView.RowFilter = "";

         // ***************************************************************
         // Insert data validation code here. Make sure to validate
 the
         // values entered by the user before converting to the appropriate
         // data types and updating the data source.
         // ***************************************************************

         // Add a new entry to replace the previous item.
         DataRow dr = Cart.NewRow();
         dr[0] = qty;
         dr[1] = item;
         // If necessary, remove the '$' character from the price before
         // converting the price to a Double.
         if(price[0] == '$')
         {
            dr[2] = Convert.ToDouble(price.Substring(1));
         }
         else
         {
            dr[2] = Convert.ToDouble(price);
         }
         Cart.Rows.Add(dr);

         // Set the EditItemIndex property to -1 to exit editing mode.
 
         // Be sure to rebind the DataList to the data source to refresh
         // the control.
         ItemsList.EditItemIndex = -1;
         BindList();

      }

   </script>

</head>
<body>

   <form runat=server>

      <h3>DataList Edit Example</h3>

      Click <b>Edit</b> to edit the values of the item.

      <br><br>
       
      <asp:DataList id="ItemsList"
           GridLines="Both"
           RepeatColumns="3"
           RepeatDirection="Horizontal"
           CellPadding="3"
           CellSpacing="0"
           OnEditCommand="Edit_Command"
           OnUpdateCommand="Update_Command"
           OnDeleteCommand="Delete_Command"
           OnCancelCommand="Cancel_Command"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <EditItemStyle BackColor="yellow">
         </EditItemStyle>

         <HeaderTemplate>

            Items

         </HeaderTemplate>
         
         <ItemTemplate>

            Item:
            <%# DataBinder.Eval(Container.DataItem, "Item") %> 

            <br>

            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "Qty") %>

            <br>

            Price:
            <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")
 %>

            <br>

            <asp:LinkButton id="EditButton" 
                 Text="Edit" 
                 CommandName="Edit"
                 runat="server"/>

         </ItemTemplate>
              
         <EditItemTemplate>

            Item:
            <asp:Label id="ItemLabel" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Item")
 %>' 
                 runat="server"/>

            <br>

            Quantity:
            <asp:TextBox id="QtyTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Qty")
 %>' 
                 runat="server"/>

            <br>

            Price:
            <asp:TextBox id="PriceTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Price",
 "{0:c}") %>' 
                 runat="server"/>

            <br>

            <asp:LinkButton id="UpdateButton" 
                 Text="Update" 
                 CommandName="Update" 
                 runat="server"/>

            <asp:LinkButton id="DeleteButton" 
                 Text="Delete" 
                 CommandName="Delete" 
                 runat="server"/>

            <asp:LinkButton id="CancelButton" 
                 Text="Cancel" 
                 CommandName="Cancel" 
                 runat="server"/>

         </EditItemTemplate>

      </asp:DataList>

   </form>

</body>
</html>

Page_Load メソッドで、イベント ハンドラ指定し、それらをプログラムによって各イベント追加する方法次のコード例示します

<%@ Page Language="VB" AutoEventWireup="True"
 %>
<%@ Import Namespace="System.Data"
 %>

<html>
<head>

   <script runat="server">

      ' The Cart and CartView objects temporarily store the data source
      ' for the DataList control while the page is being processed.
      Dim Cart As DataTable = New
 DataTable()
      Dim CartView As DataView
 
      Sub Page_Load(sende As Object,
 e As EventArgs) 
 
         ' With a database, use an select query to retrieve the data.
         ' Because the data source in this example is an in-memory
         ' DataTable, retrieve the data from session state if it exists;
         ' otherwise, create the data source.
         GetSource()

         ' The DataGrid control maintains state between posts to the
 server;
         ' it only needs to be bound to a data source the first time
 the
         ' page is loaded or when the data source is updated.
         If Not IsPostBack Then

            BindList()

         End If

         ' Manually register the event-handling methods.
         AddHandler ItemsList.EditCommand, AddressOf
 Edit_Command
         AddHandler ItemsList.UpdateCommand, AddressOf
 Update_Command
         AddHandler ItemsList.DeleteCommand, AddressOf
 Delete_Command
         AddHandler ItemsList.CancelCommand, AddressOf
 Cancel_Command
                   
      End Sub

      Sub BindList() 

         ' Set the data source and bind to the DataList control.
         ItemsList.DataSource = CartView
         ItemsList.DataBind()

      End Sub

      Sub GetSource()

         ' For this example, the data source is a DataTable that
         ' is stored in session state. If the data source does not exist
,
         ' create it; otherwise, load the data.
         If Session("ShoppingCart")
 Is Nothing Then 

            ' Create the sample data.
            Dim dr As DataRow  
 
            ' Define the columns of the table.
            Cart.Columns.Add(new DataColumn("Qty",
 GetType(Int32)))
            Cart.Columns.Add(new DataColumn("Item",
 GetType(String)))
            Cart.Columns.Add(new DataColumn("Price",
 GetType(Double)))

            ' Store the table in session state to persist its values
            ' between posts to the server.
            Session("ShoppingCart") = Cart
             
            ' Populate the DataTable with sample data.
            Dim i As Integer

            For i = 1 To 9 
            
               dr = Cart.NewRow()
               If (i Mod 2) <> 0 Then

                  dr(0) = 2
               
               Else
               
                  dr(0) = 1
               
               End If

               dr(1) = "Item " & i.ToString()
               dr(2) = (1.23 * (i + 1))
               Cart.Rows.Add(dr)
            
            Next i

         Else

            ' Retrieve the sample data from session state.
            Cart = CType(Session("ShoppingCart"),
 DataTable)

         End If         
 
         ' Create a DataView and specify the field to sort by.
         CartView = New DataView(Cart)
         CartView.Sort="Item"

         Return

      End Sub

      Sub Edit_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Set the EditItemIndex property to the index of the item clicked
         ' in the DataList control to enable editing for that item.
 Be sure
         ' to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = e.Item.ItemIndex
         BindList()

      End Sub

      Sub Cancel_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Set the EditItemIndex property to -1 to exit editing mode.
 Be sure
         ' to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1
         BindList()

      End Sub

      Sub Delete_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Retrieve the name of the item to remove.
         Dim item As String
 = (CType(e.Item.FindControl("ItemLabel"), Label)).Text

         ' Filter the CartView for the selected item and remove it from
 
         ' the data source.
         CartView.RowFilter = "Item='" & item & "'"
         If CartView.Count > 0 Then 
       
            CartView.Delete(0)
         
         End If
         CartView.RowFilter = ""

         ' Set the EditItemIndex property to -1 to exit editing mode.
 Be sure 
         ' to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1
         BindList()

      End Sub

      Sub Update_Command(sender As Object,
 e As DataListCommandEventArgs) 

         ' Retrieve the updated values from the selected item.
         Dim item As String
 = _
             (CType(e.Item.FindControl("ItemLabel"),
 Label)).Text
         Dim qty As String
 = _
             (CType(e.Item.FindControl("QtyTextBox"),
 TextBox)).Text
         Dim price As String
 = _
             (CType(e.Item.FindControl("PriceTextBox"),
 TextBox)).Text

         ' With a database, use an update command to update the data.
         ' Because the data source in this example is an in-memory 
         ' DataTable, delete the old row and replace it with a new one.

         ' Filter the CartView for the selected item and remove it from
 
         ' the data source.
         CartView.RowFilter = "Item='" & item & "'"
         If CartView.Count > 0 Then 
       
            CartView.Delete(0)
         
         End If
         CartView.RowFilter = ""

         ' ***************************************************************
         ' Insert data validation code here. Make sure to validate the
         ' values entered by the user before converting to the appropriate
         ' data types and updating the data source.
         ' ***************************************************************

         ' Add a new entry to replace the previous item.
         Dim dr As DataRow = Cart.NewRow()
         dr(0) = qty
         dr(1) = item
         ' If necessary, remove the '$' character from the price before
 
         ' converting the price to a Double.
         If price.Chars(0) = "$"
 Then

            dr(2) = Convert.ToDouble(price.Substring(1))

         Else

            dr(2) = Convert.ToDouble(price)
         
         End If

         Cart.Rows.Add(dr)

         ' Set the EditItemIndex property to -1 to exit editing mode.
 
         ' Be sure to rebind the DataList to the data source to refresh
 
         ' the control.
         ItemsList.EditItemIndex = -1
         BindList()

      End Sub

   </script>

</head>
<body>

   <form runat=server>

      <h3>DataList Edit Example</h3>

      Click <b>Edit</b> to edit the values of
 the item.

      <br><br>
       
      <asp:DataList id="ItemsList"
           GridLines="Both"
           RepeatColumns="3"
           RepeatDirection="Horizontal"
           CellPadding="3"
           CellSpacing="0"
           OnEditCommand="Edit_Command"
           OnUpdateCommand="Update_Command"
           OnDeleteCommand="Delete_Command"
           OnCancelCommand="Cancel_Command"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <EditItemStyle BackColor="yellow">
         </EditItemStyle>

         <HeaderTemplate>

            Items

         </HeaderTemplate>
         
         <ItemTemplate>

            Item:
            <%# DataBinder.Eval(Container.DataItem, "Item")
 %> 

            <br>

            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "Qty")
 %>

            <br>

            Price:
            <%# DataBinder.Eval(Container.DataItem, "Price",
 "{0:c}") %>

            <br>

            <asp:LinkButton id="EditButton" 
                 Text="Edit" 
                 CommandName="Edit"
                 runat="server"/>

         </ItemTemplate>
              
         <EditItemTemplate>

            Item:
            <asp:Label id="ItemLabel" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Item")
 %>' 
                 runat="server"/>

            <br>

            Quantity:
            <asp:TextBox id="QtyTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Qty")
 %>' 
                 runat="server"/>

            <br>

            Price:
            <asp:TextBox id="PriceTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Price",
 "{0:c}") %>' 
                 runat="server"/>

            <br>

            <asp:LinkButton id="UpdateButton" 
                 Text="Update" 
                 CommandName="Update" 
                 runat="server"/>

            <asp:LinkButton id="DeleteButton" 
                 Text="Delete" 
                 CommandName="Delete" 
                 runat="server"/>

            <asp:LinkButton id="CancelButton" 
                 Text="Cancel" 
                 CommandName="Cancel" 
                 runat="server"/>

         </EditItemTemplate>

      </asp:DataList>

   </form>

</body>
</html>

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>
<head>

   <script runat="server">

      // The Cart and CartView objects temporarily store the data source
      // for the DataList control while the page is being processed.
      DataTable Cart = new DataTable();
      DataView CartView;   
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         // With a database, use an select query to retrieve the data.
 
         // Because the data source in this example is an in-memory
         // DataTable, retrieve the data from session state if it exists;
         // otherwise, create the data source.
         GetSource();

         // The DataList control maintains state between posts to the
 server;
         // it only needs to be bound to a data source the first time
 the
         // page is loaded or when the data source is updated.
         if (!IsPostBack)
         {

            BindList();

         }

         // Manually register the event-handling methods.
         ItemsList.EditCommand += 
             new DataListCommandEventHandler(this.Edit_Command);
         ItemsList.UpdateCommand += 
             new DataListCommandEventHandler(this.Update_Command);
         ItemsList.DeleteCommand += 
             new DataListCommandEventHandler(this.Delete_Command);
         ItemsList.CancelCommand += 
             new DataListCommandEventHandler(this.Cancel_Command);
                   
      }

      void BindList() 
      {

         // Set the data source and bind to the DataList control.
         ItemsList.DataSource = CartView;
         ItemsList.DataBind();

      }

      void GetSource()
      {

         // For this example, the data source is a DataTable that 
         // is stored in session state. If the data source does not
 exist,
         // create it; otherwise, load the data.
         if (Session["ShoppingCart"] == null)
 
         {     

            // Create the sample data.
            DataRow dr;  
 
            // Define the columns of the table.
            Cart.Columns.Add(new DataColumn("Qty", typeof(Int32)));
            Cart.Columns.Add(new DataColumn("Item",
 typeof(String)));
            Cart.Columns.Add(new DataColumn("Price",
 typeof(Double)));

            // Store the table in session state to persist its values
            // between posts to the server.
            Session["ShoppingCart"] = Cart;
             
            // Populate the DataTable with sample data.
            for (int i = 1; i <= 9; i++)
 
            {
               dr = Cart.NewRow();
               if (i % 2 != 0)
               {
                  dr[0] = 2;
               }
               else
               {
                  dr[0] = 1;
               }
               dr[1] = "Item " + i.ToString();
               dr[2] = (1.23 * (i + 1));
               Cart.Rows.Add(dr);
            }

         } 

         else
         {

            // Retrieve the sample data from session state.
            Cart = (DataTable)Session["ShoppingCart"];

         }         
 
         // Create a DataView and specify the field to sort by.
         CartView = new DataView(Cart);
         CartView.Sort="Item";

         return;

      }

      void Edit_Command(Object sender, DataListCommandEventArgs
 e) 
      {

         // Set the EditItemIndex property to the index of the item
 clicked
         // in the DataList control to enable editing for that item.
 Be sure
         // to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = e.Item.ItemIndex;
         BindList();

      }

      void Cancel_Command(Object sender, DataListCommandEventArgs
 e) 
      {

         // Set the EditItemIndex property to -1 to exit editing mode.
 Be sure
         // to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1;
         BindList();

      }

      void Delete_Command(Object sender, DataListCommandEventArgs
 e) 
      { 

         // Retrieve the name of the item to remove.
         String item = ((Label)e.Item.FindControl("ItemLabel")).Text;

         // Filter the CartView for the selected item and remove it
 from
         // the data source.
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0) 
         {
            CartView.Delete(0);
         }
         CartView.RowFilter = "";

         // Set the EditItemIndex property to -1 to exit editing mode.
 Be sure
         // to rebind the DataList to the data source to refresh the
 control.
         ItemsList.EditItemIndex = -1;
         BindList();

      }

      void Update_Command(Object sender, DataListCommandEventArgs
 e) 
      {

         // Retrieve the updated values from the selected item.
         String item = ((Label)e.Item.FindControl("ItemLabel")).Text;
         String qty = ((TextBox)e.Item.FindControl("QtyTextBox")).Text;
         String price = ((TextBox)e.Item.FindControl("PriceTextBox")).Text;

         // With a database, use an update command to update the data.
         // Because the data source in this example is an in-memory
 
         // DataTable, delete the old row and replace it with a new
 one.

         // Filter the CartView for the selected item and remove it
 from
         // the data source.
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0)
         {
            CartView.Delete(0);
         }
         CartView.RowFilter = "";

         // ***************************************************************
         // Insert data validation code here. Make sure to validate
 the
         // values entered by the user before converting to the appropriate
         // data types and updating the data source.
         // ***************************************************************

         // Add a new entry to replace the previous item.
         DataRow dr = Cart.NewRow();
         dr[0] = qty;
         dr[1] = item;
         // If necessary, remove the '$' character from the price before
         // converting the price to a Double.
         if(price[0] == '$')
         {
            dr[2] = Convert.ToDouble(price.Substring(1));
         }
         else
         {
            dr[2] = Convert.ToDouble(price);
         }
         Cart.Rows.Add(dr);

         // Set the EditItemIndex property to -1 to exit editing mode.
 
         // Be sure to rebind the DataList to the data source to refresh
         // the control.
         ItemsList.EditItemIndex = -1;
         BindList();

      }

   </script>

</head>
<body>

   <form runat=server>

      <h3>DataList Edit Example</h3>

      Click <b>Edit</b> to edit the values of the item.

      <br><br>
       
      <asp:DataList id="ItemsList"
           GridLines="Both"
           RepeatColumns="3"
           RepeatDirection="Horizontal"
           CellPadding="3"
           CellSpacing="0"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <EditItemStyle BackColor="yellow">
         </EditItemStyle>

         <HeaderTemplate>

            Items

         </HeaderTemplate>
         
         <ItemTemplate>

            Item:
            <%# DataBinder.Eval(Container.DataItem, "Item") %> 

            <br>

            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "Qty") %>

            <br>

            Price:
            <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")
 %>

            <br>

            <asp:LinkButton id="EditButton" 
                 Text="Edit" 
                 CommandName="Edit"
                 runat="server"/>

         </ItemTemplate>
              
         <EditItemTemplate>

            Item:
            <asp:Label id="ItemLabel" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Item")
 %>' 
                 runat="server"/>

            <br>

            Quantity:
            <asp:TextBox id="QtyTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Qty")
 %>' 
                 runat="server"/>

            <br>

            Price:
            <asp:TextBox id="PriceTextBox" 
                 Text='<%# DataBinder.Eval(Container.DataItem, "Price",
 "{0:c}") %>' 
                 runat="server"/>

            <br>

            <asp:LinkButton id="UpdateButton" 
                 Text="Update" 
                 CommandName="Update" 
                 runat="server"/>

            <asp:LinkButton id="DeleteButton" 
                 Text="Delete" 
                 CommandName="Delete" 
                 runat="server"/>

            <asp:LinkButton id="CancelButton" 
                 Text="Cancel" 
                 CommandName="Cancel" 
                 runat="server"/>

         </EditItemTemplate>

      </asp:DataList>

   </form>

</body>
</html>

.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.EventArgs
     System.Web.UI.WebControls.CommandEventArgs
      System.Web.UI.WebControls.DataListCommandEventArgs
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataListCommandEventArgs メンバ
System.Web.UI.WebControls 名前空間
DataListCommandEventHandler
DataList クラス
DataList.CancelCommand イベント
DataList.DeleteCommand イベント
DataList.EditCommand イベント
DataList.ItemCommand イベント
DataList.UpdateCommand イベント
その他の技術情報
DataList Web サーバー コントロール

DataListCommandEventArgs コンストラクタ

DataListCommandEventArgs クラス新しインスタンス初期化します。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Sub New ( _
    item As DataListItem, _
    commandSource As Object, _
    originalArgs As CommandEventArgs _
)
Dim item As DataListItem
Dim commandSource As Object
Dim originalArgs As CommandEventArgs

Dim instance As New DataListCommandEventArgs(item,
 commandSource, originalArgs)
public DataListCommandEventArgs (
    DataListItem item,
    Object commandSource,
    CommandEventArgs originalArgs
)
public:
DataListCommandEventArgs (
    DataListItem^ item, 
    Object^ commandSource, 
    CommandEventArgs^ originalArgs
)
public DataListCommandEventArgs (
    DataListItem item, 
    Object commandSource, 
    CommandEventArgs originalArgs
)
public function DataListCommandEventArgs (
    item : DataListItem, 
    commandSource : Object, 
    originalArgs : CommandEventArgs
)

パラメータ

item

DataList で選択された項目。

commandSource

コマンドソース

originalArgs

元のイベント データ格納している CommandEventArgs。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataListCommandEventArgs クラス
DataListCommandEventArgs メンバ
System.Web.UI.WebControls 名前空間
その他の技術情報
DataList Web サーバー コントロール

DataListCommandEventArgs プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ CommandArgument  コマンド引数取得します。 ( CommandEventArgs から継承されます。)
パブリック プロパティ CommandName  コマンド名を取得します。 ( CommandEventArgs から継承されます。)
パブリック プロパティ CommandSource コマンドソース取得します
パブリック プロパティ Item DataList コントロールコマンドソース格納している項目を取得します
参照参照

関連項目

DataListCommandEventArgs クラス
System.Web.UI.WebControls 名前空間
DataListCommandEventHandler
DataList クラス
DataList.CancelCommand イベント
DataList.DeleteCommand イベント
DataList.EditCommand イベント
DataList.ItemCommand イベント
DataList.UpdateCommand イベント

その他の技術情報

DataList Web サーバー コントロール

DataListCommandEventArgs メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataListCommandEventArgs クラス
System.Web.UI.WebControls 名前空間
DataListCommandEventHandler
DataList クラス
DataList.CancelCommand イベント
DataList.DeleteCommand イベント
DataList.EditCommand イベント
DataList.ItemCommand イベント
DataList.UpdateCommand イベント

その他の技術情報

DataList Web サーバー コントロール

DataListCommandEventArgs メンバ

DataList コントロールの CancelCommand、DeleteCommand、EditCommand、ItemCommand、UpdateCommand の各イベントデータ提供します。このクラス継承できません。

DataListCommandEventArgs データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド DataListCommandEventArgs DataListCommandEventArgs クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ CommandArgument  コマンド引数取得します。(CommandEventArgs から継承されます。)
パブリック プロパティ CommandName  コマンド名を取得します。(CommandEventArgs から継承されます。)
パブリック プロパティ CommandSource コマンドソース取得します
パブリック プロパティ Item DataList コントロールコマンドソース格納している項目を取得します
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataListCommandEventArgs クラス
System.Web.UI.WebControls 名前空間
DataListCommandEventHandler
DataList クラス
DataList.CancelCommand イベント
DataList.DeleteCommand イベント
DataList.EditCommand イベント
DataList.ItemCommand イベント
DataList.UpdateCommand イベント

その他の技術情報

DataList Web サーバー コントロール


このページでは「.NET Framework クラス ライブラリ リファレンス」からDataListCommandEventArgsを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDataListCommandEventArgsを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDataListCommandEventArgs を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataListCommandEventArgs」の関連用語

DataListCommandEventArgsのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataListCommandEventArgsのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS