FormView.SelectedValue Właściwość

Definicja

Pobiera wartość klucza danych bieżącego rekordu w kontrolce FormView .

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

Wartość właściwości

Wartość klucza danych bieżącego rekordu w kontrolce FormView .

Atrybuty

Przykłady

W poniższym przykładzie pokazano, jak za pomocą SelectedValue właściwości określić wartość pola klucza.


<%@ 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">
  
  void EmployeeFormView_ItemCreated(Object sender, EventArgs e)
  {
    MessageLabel.Text = "The key value is " + EmployeeFormView.SelectedValue.ToString() + ".";
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView SelectedValue Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView SelectedValue Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        onitemcreated="EmployeeFormView_ItemCreated"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
          </table>
        
        </itemtemplate>
          
      </asp:formview>
      
      <br/><br/>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>
          
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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">
  
  Sub EmployeeFormView_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
  
    MessageLabel.Text = "The key value is " & EmployeeFormView.SelectedValue.ToString() & "."
  
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView SelectedValue Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView SelectedValue Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        onitemcreated="EmployeeFormView_ItemCreated"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
          </table>
        
        </itemtemplate>
          
      </asp:formview>
      
      <br/><br/>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>
          
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

Uwagi

Po ustawieniu DataKeyNamesFormView właściwości kontrolka automatycznie tworzy DataKey obiekt zawierający pary klucz/wartość pola lub pola wymienione we DataKeyNames właściwości bieżącego rekordu. DataKey Obiekt reprezentuje pole klucza podstawowego lub pola dla bieżącego rekordu DataKey i jest przechowywane we właściwości. Zamiast używać DataKey właściwości w celu uzyskania dostępu do wartości klucza danych, można użyć SelectedValue właściwości jako skrótu.

Uwaga

Gdy DataKeyNames właściwość wyświetla listę wielu pól, ta właściwość zwraca wartość pierwszego pola na liście.

Dotyczy

Zobacz też