ListView.SelectedValue Eigenschaft

Definition

Ruft den Datenschlüsselwert des ausgewählten Elements in einem ListView-Steuerelement ab.

public:
 property System::Object ^ SelectedValue { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public object SelectedValue { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : obj
Public ReadOnly Property SelectedValue As Object

Eigenschaftswert

Object

Der Datenschlüsselwert des ausgewählten Elements in einem ListView-Steuerelement.

Attribute

Beispiele

Das folgende Beispiel zeigt, wie Sie das erste Schlüsselfeld als Parameter in einem Master-/Detailszenario verwenden. Wenn ein Element im ListView Steuerelement ausgewählt ist, wird die SelectedValue Eigenschaft als PropertyName Objekt ControlParameter verwendet. Dieses Parameterobjekt wird wiederum vom SqlDataSource Steuerelement verwendet, an das das zweite ListView Steuerelement gebunden ist.

<%@ 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">
  protected void CategoriesListView_OnItemDeleting(object sender, ListViewDeleteEventArgs e)
  {
    if (SubCategoriesGridView.Rows.Count > 0)
    {
      MessageLabel.Text = "You cannot delete a category that has sub-categories.";
      e.Cancel = true;
    }
  }

  protected void Page_Load()
  {
    MessageLabel.Text = String.Empty;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Subcategories List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <b>Categories</b>
      <br />
      
      <asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
      
      <asp:ListView runat="server" 
        ID="CategoriesListView"
        OnItemDeleting="CategoriesListView_OnItemDeleting"
        DataSourceID="CategoriesDataSource" 
        DataKeyNames="ProductCategoryID">
        <LayoutTemplate>
          <table runat="server" id="tblCategories" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Select" CommandName="Select" />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr runat="server" style="background-color:#90EE90">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Delete" CommandName="Delete" />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>
      
      <br />
      
      <b>Subcategories</b>
      <asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
           DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID" 
           AutoGenerateColumns="True" />
       
      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="CategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductCategoryID], [Name]
                       FROM Production.ProductCategory">
      </asp:SqlDataSource>
      <asp:SqlDataSource ID="SubCategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductSubcategoryID], [Name]
                       FROM Production.ProductSubcategory
                       WHERE ProductCategoryID = @ProductCategoryID
                       ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
                 ControlID="CategoriesListView" PropertyName="SelectedValue"  />
          </SelectParameters>
      </asp:SqlDataSource>
    </form>
  </body>
</html>
<%@ Page language="VB" %>

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

<script runat="server">
    Protected Sub CategoriesListView_OnItemDeleting(sender As Object, e As ListViewDeleteEventArgs)
        If SubCategoriesGridView.Rows.Count > 0 Then
            MessageLabel.Text = "You cannot delete a category that has sub-categories."
            e.Cancel = True
        End If
    End Sub

    Protected Sub Page_Load()
        MessageLabel.Text = String.Empty
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Subcategories List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <b>Categories</b>
      <br />
      
      <asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
      
      <asp:ListView runat="server" 
        ID="CategoriesListView"
        OnItemDeleting="CategoriesListView_OnItemDeleting"
        DataSourceID="CategoriesDataSource" 
        DataKeyNames="ProductCategoryID">
        <LayoutTemplate>
          <table runat="server" id="tblCategories" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Select" CommandName="Select" />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr runat="server" style="background-color:#90EE90">
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td style="width:40px">
              <asp:LinkButton runat="server" ID="SelectCategoryButton" 
                Text="Delete" CommandName="Delete" />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>
      
      <br />
      
      <b>Subcategories</b>
      <asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
           DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID" 
           AutoGenerateColumns="True" />
       
      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="CategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductCategoryID], [Name]
                       FROM Production.ProductCategory">
      </asp:SqlDataSource>
      <asp:SqlDataSource ID="SubCategoriesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductSubcategoryID], [Name]
                       FROM Production.ProductSubcategory
                       WHERE ProductCategoryID = @ProductCategoryID
                       ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
                 ControlID="CategoriesListView" PropertyName="SelectedValue"  />
          </SelectParameters>
      </asp:SqlDataSource>
    </form>
  </body>
</html>

Das folgende Beispiel zeigt, wie Sie die SelectedValue Eigenschaft verwenden, um den Wert des Schlüsselfelds zu bestimmen.

<%@ 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">

  protected void DepartmentsListView_SelectedIndexChanged(object sender, EventArgs e)
  {
    MessageLabel.Text = "The key value is " + 
      DepartmentsListView.SelectedValue.ToString() + ".";
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView SelectedValue Example</title>
    <style type="text/css">
      .header
      {
        border: 1px solid #008080;
        background-color: #008080;
        color: White;
      }
      .item td { border: 1px solid #008080; }
      .selection td  
      {
        border: 1px solid #008080; 
        background-color: #7FFF00;
      }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
    
      <h3>ListView SelectedValue Example</h3>
            
      <asp:ListView runat="server" 
        ID="DepartmentsListView"
        DataSourceID="DepartmentDataSource" 
        DataKeyNames="DepartmentID" 
        OnSelectedIndexChanged="DepartmentsListView_SelectedIndexChanged">
        <LayoutTemplate>
          <b>Department List</b>
          <br />
          <table width="500px" runat="server" id="tblDepartments">
            <tr class="header" runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">Department Name</th>
              <th runat="server">Group Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr class="item" runat="server">
            <td>
              <asp:LinkButton runat="server" 
                ID="SelectButton" 
                Text="Select"
                CommandName="Select" />
            </td>
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="GroupNameLabel" Text='<%#Eval("GroupName") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr class="selection" runat="server">
            <td>&nbsp;</td>
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="GroupNameLabel" Text='<%#Eval("GroupName") %>' />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>
      <br/>
      
      <asp:Label ID="MessageLabel"
        ForeColor="Red"
        runat="server"/>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="DepartmentDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT DepartmentID, Name, GroupName 
          FROM HumanResources.Department">
      </asp:SqlDataSource>
                       
    </form>
  </body>
</html>
<%@ Page language="VB" %>

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

<script runat="server">

  Protected Sub DepartmentsListView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 
  
    MessageLabel.Text = "The key value is " & _
      DepartmentsListView.SelectedValue.ToString() & "."

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView SelectedValue Example</title>
    <style type="text/css">
      .header
      {
        border: 1px solid #008080;
        background-color: #008080;
        color: White;
      }
      .item td { border: 1px solid #008080; }
      .selection td  
      {
        border: 1px solid #008080; 
        background-color: #7FFF00;
      }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
    
      <h3>ListView SelectedValue Example</h3>
            
      <asp:ListView runat="server" 
        ID="DepartmentsListView"
        DataSourceID="DepartmentDataSource" 
        DataKeyNames="DepartmentID" 
        OnSelectedIndexChanged="DepartmentsListView_SelectedIndexChanged">
        <LayoutTemplate>
          <b>Department List</b>
          <br />
          <table width="500px" runat="server" id="tblDepartments">
            <tr class="header" runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">Department Name</th>
              <th runat="server">Group Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr class="item" runat="server">
            <td>
              <asp:LinkButton runat="server" 
                ID="SelectButton" 
                Text="Select"
                CommandName="Select" />
            </td>
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="GroupNameLabel" Text='<%#Eval("GroupName") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <SelectedItemTemplate>
          <tr class="selection" runat="server">
            <td>&nbsp;</td>
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="GroupNameLabel" Text='<%#Eval("GroupName") %>' />
            </td>
          </tr>
        </SelectedItemTemplate>
      </asp:ListView>
      <br/>
      
      <asp:Label ID="MessageLabel"
        ForeColor="Red"
        runat="server"/>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="DepartmentDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT DepartmentID, Name, GroupName 
          FROM HumanResources.Department">
      </asp:SqlDataSource>
                       
    </form>
  </body>
</html>

Hinweise

Die DataKeyNames Eigenschaft kann auf eine durch Kommas getrennte Liste von Feldnamen festgelegt werden, die den Primärschlüssel der Datenquelle darstellen. Wenn es festgelegt ist, erstellt das ListView Steuerelement automatisch ein DataKey Objekt für jedes Element im Steuerelement mithilfe des Werts oder der Werte des angegebenen Felds oder der angegebenen Felder. Die DataKey Objekte werden dann der Auflistung des Steuerelements DataKeys hinzugefügt.

Normalerweise wird die DataKeys Eigenschaft verwendet, um das DataKey Objekt für ein bestimmtes Datenelement im ListView Steuerelement abzurufen. Wenn Sie jedoch nur das DataKey Objekt des aktuell ausgewählten Elements abrufen möchten, können Sie die SelectedDataKey Eigenschaft als Verknüpfung verwenden. Sie können auch direkt den Datenschlüsselwert des ersten Schlüsselfelds des ausgewählten Elements mithilfe der SelectedValue Eigenschaft bestimmen.

Wenn Sie ein ControlParameter Objekt erstellen und auf ein anderes Schlüsselfeld als das erste Feld zugreifen möchten, verwenden Sie die SelectedDataKey Eigenschaft. Ein Beispiel finden Sie in der SelectedDataKey Eigenschaft.

Gilt für

Siehe auch