Udostępnij przez


ButtonField.DataTextFormatString Właściwość

Definicja

Pobiera lub ustawia ciąg określający format wyświetlania dla wartości pola.

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

Wartość właściwości

Ciąg formatu określający format wyświetlania wartości pola. Wartość domyślna to pusty ciąg (""), który wskazuje, że do wartości pola nie zastosowano żadnego specjalnego formatowania.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą DataTextFormatString właściwości określić niestandardowy format wyświetlania wartości pola.


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

Uwagi

Użyj właściwości , DataTextFormatString aby określić niestandardowy format wyświetlania dla wartości wyświetlanych w ButtonField obiekcie. DataTextFormatString Jeśli właściwość nie jest ustawiona, wartość pola jest wyświetlana bez specjalnego formatowania.

Uwaga

Ciąg formatu jest stosowany tylko wtedy, gdy właściwość jest ustawiona DataTextField .

Ciąg formatu może być dowolnym ciągiem literału i zwykle zawiera symbol zastępczy wartości pola. Na przykład w ciągu "Item Value: {0}"{0} formatu symbol zastępczy jest zastępowany wartością pola po wyświetleniu ButtonField go w obiekcie. Pozostała część ciągu formatu jest wyświetlana jako tekst literału.

Uwaga

Jeśli ciąg formatu nie zawiera symbolu zastępczego, wartość pola ze źródła danych nie jest uwzględniona w ostatnim tekście wyświetlania.

Symbol zastępczy składa się z dwóch części rozdzielonych dwukropkiem i opakowanych w nawiasy klamrowe w postaci { : ABxx }. Wartość przed dwukropkiem (A w ogólnym przykładzie) określa indeks wartości pola na liście parametrów opartych na zera.

Uwaga

Ten A parametr jest częścią składni formatowania. Ponieważ w każdej komórce istnieje tylko jedna wartość pola, tę wartość można ustawić tylko na wartość 0.

Dwukropek i wartości po dwukropku są opcjonalne. Znak po dwukropku (B w ogólnym przykładzie) określa format, w którym ma być wyświetlana wartość. W poniższej tabeli wymieniono typowe formaty.

Formatuj znak Opis
C Wyświetla wartości liczbowe w formacie waluty.
D Wyświetla wartości liczbowe w formacie dziesiętny.
E Wyświetla wartości liczbowe w formacie naukowym (wykładniczym).
F Wyświetla wartości liczbowe w stałym formacie.
G Wyświetla wartości liczbowe w formacie ogólnym.
N Wyświetla wartości liczbowe w formacie liczbowym.
X Wyświetla wartości liczbowe w formacie szesnastkowym.

Uwaga

XZ wyjątkiem znaków formatu nie uwzględnia się wielkości liter. Znak X formatu wyświetla znaki szesnastkowe w określonym przypadku.

Wartość po znaku formatu (xx w ogólnym przykładzie) określa liczbę cyfr znaczących lub miejsc dziesiętnych do wyświetlenia. Na przykład ciąg "{0:F2}" formatu wyświetla liczbę o stałym punkcie z dwoma miejscami dziesiętnymi.

Aby uzyskać więcej informacji na temat formatowania ciągów, zobacz Formatting Types (Typy formatowania).

Wartość tej właściwości jest przechowywana w stanie widoku.

Dotyczy

Zobacz też