Share via


ButtonField.DataTextFormatString Vlastnost

Definice

Získá nebo nastaví řetězec, který určuje formát zobrazení pro hodnotu pole.

public:
 virtual property System::String ^ DataTextFormatString { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataTextFormatString { get; set; }
member this.DataTextFormatString : string with get, set
Public Overridable Property DataTextFormatString As String

Hodnota vlastnosti

Formátovací řetězec, který určuje formát zobrazení pro hodnotu pole. Výchozí hodnota je prázdný řetězec (""), který označuje, že na hodnotu pole není použito žádné zvláštní formátování.

Příklady

Následující příklad kódu ukazuje, jak použít DataTextFormatString vlastnost k určení vlastního formátu zobrazení pro hodnoty pole.


<%@ 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 AuthorsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
  
    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Select")
    {
    
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);    
    
      // Get the last name of the selected author from the appropriate
      // cell in the GridView control.
      GridViewRow selectedRow = AuthorsGridView.Rows[index];
      TableCell lastNameCell = selectedRow.Cells[1];
      string lastName = lastNameCell.Text;  
    
      // Display the selected author.
      Message.Text = "You selected " + lastName + ".";
      
    }
    
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField DataTextField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ButtonField DataTextField Example</h3>
      
      <asp:label id="Message"
        forecolor="Red"
        runat="server"
        AssociatedControlID="AuthorsGridView"/>
                    
      <!-- Set the DataTextField property of the ButtonField -->
      <!-- declaratively. Set the DataTextFormatString       -->
      <!-- property to apply special formatting to the text. -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="AuthorsGridView_RowCommand" 
        runat="server">
                
        <columns>
                
          <asp:buttonfield buttontype="Link" 
            commandname="Select"
            headertext="Select Author"
            datatextfield="au_lname"
            datatextformatstring="[{0}]"    
            text="Select"/>
          <asp:boundfield datafield="au_fname" 
            headertext="First Name"/>
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </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">

  Sub AuthorsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
  
    ' If multiple ButtonField column fields are used, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Select" Then
    
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
      ' Get the last name of the selected author from the appropriate
      ' cell in the GridView control.
      Dim selectedRow As GridViewRow = AuthorsGridView.Rows(index)
      Dim lastNameCell As TableCell = selectedRow.Cells(1)
      Dim lastName As String = lastNameCell.Text
    
      ' Display the selected author.
      Message.Text = "You selected " & lastName & "."
      
    End If
    
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField DataTextField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ButtonField DataTextField Example</h3>
      
      <asp:label id="Message"
        forecolor="Red"
        runat="server"
        AssociatedControlID="AuthorsGridView"/>
                    
      <!-- Set the DataTextField property of the ButtonField -->
      <!-- declaratively. Set the DataTextFormatString       -->
      <!-- property to apply special formatting to the text. -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="AuthorsGridView_RowCommand"
        runat="server">
                
        <columns>
                
          <asp:buttonfield buttontype="Link" 
            commandname="Select"
            headertext="Select Author"
            datatextfield="au_lname"
            datatextformatstring="[{0}]"    
            text="Select"/>
          <asp:boundfield datafield="au_fname" 
            headertext="First Name"/>
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Poznámky

DataTextFormatString Pomocí vlastnosti určete vlastní formát zobrazení pro hodnoty, které jsou zobrazeny v objektuButtonField. DataTextFormatString Pokud vlastnost není nastavená, zobrazí se hodnota pole bez speciálního formátování.

Poznámka

Formátovací řetězec se použije pouze v případech, kdy je vlastnost nastavena DataTextField .

Formátovací řetězec může být libovolný řetězec literálu a obvykle obsahuje zástupný symbol pro hodnotu pole. Například ve formátovacím řetězci "Item Value: {0}"{0} se zástupný symbol při zobrazení v objektu ButtonField nahradí hodnotou pole. Zbytek řetězce formátu se zobrazí jako text literálu.

Poznámka

Pokud formátovací řetězec neobsahuje zástupný symbol, hodnota pole ze zdroje dat není zahrnuta do konečného zobrazovaného textu.

Zástupný symbol se skládá ze dvou částí oddělených dvojtečkami a zabalených do závorek ve tvaru { A : Bxx }. Hodnota před dvojtečku (A v obecném příkladu) určuje index hodnoty pole v seznamu parametrů založených na nule.

Poznámka

Tento A parametr je součástí syntaxe formátování. Vzhledem k tomu, že v každé buňce je pouze jedna hodnota pole, může být tato hodnota nastavena pouze na hodnotu 0.

Dvojtečka a hodnoty za dvojtečka jsou volitelné. Znak za dvojtečku (B v obecném příkladu) určuje formát, ve kterém se má hodnota zobrazit. V následující tabulce jsou uvedené běžné formáty.

Formát znaku Description
C Zobrazí číselné hodnoty ve formátu měny.
D Zobrazí číselné hodnoty v desítkovém formátu.
E Zobrazí číselné hodnoty ve vědeckém (exponenciálním) formátu.
F Zobrazí číselné hodnoty v pevném formátu.
G Zobrazí číselné hodnoty v obecném formátu.
N Zobrazí číselné hodnoty ve formátu čísel.
X Zobrazí číselné hodnoty v šestnáctkovém formátu.

Poznámka

S výjimkou Xformátu se ve znaménách nerozlišují velká a malá písmena. Formátovací X znak zobrazí šestnáctkové znaky v zadaném případě.

Hodnota za znakem formátu (xx v obecném příkladu) určuje počet platných číslic nebo desetinných míst, která se mají zobrazit. Formátovací řetězec "{0:F2}" například zobrazí číslo s pevným bodem se dvěma desetinnými místy.

Další informace o formátování řetězců najdete v tématu Typy formátování.

Hodnota této vlastnosti je uložena ve stavu zobrazení.

Platí pro

Viz také