Bagikan melalui


ButtonField.DataTextFormatString Properti

Definisi

Mendapatkan atau mengatur string yang menentukan format tampilan untuk nilai bidang.

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

Nilai Properti

String format yang menentukan format tampilan untuk nilai bidang . Defaultnya adalah string kosong (""), yang menunjukkan bahwa tidak ada pemformatan khusus yang diterapkan ke nilai bidang.

Contoh

Contoh kode berikut menunjukkan cara menggunakan DataTextFormatString properti untuk menentukan format tampilan kustom untuk nilai bidang.


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

Keterangan

DataTextFormatString Gunakan properti untuk menentukan format tampilan kustom untuk nilai yang ditampilkan dalam ButtonField objek. DataTextFormatString Jika properti tidak diatur, nilai untuk bidang ditampilkan tanpa pemformatan khusus.

Nota

String format hanya diterapkan saat DataTextField properti diatur.

String format dapat berupa string harfiah apa pun dan biasanya menyertakan tempat penampung untuk nilai untuk bidang . Misalnya, dalam string "Item Value: {0}"format , {0} tempat penampung diganti dengan nilai untuk bidang saat ditampilkan dalam ButtonField objek. String format lainnya ditampilkan sebagai teks harfiah.

Nota

Jika string format tidak menyertakan tempat penampung, nilai untuk bidang dari sumber data tidak disertakan dalam teks tampilan akhir.

Tempat penampung terdiri dari dua bagian, dipisahkan oleh titik dua dan dibungkus dengan kurung kurawal, dalam bentuk { A : Bxx }. Nilai sebelum titik dua (A dalam contoh umum) menentukan indeks untuk nilai bidang dalam daftar parameter berbasis nol.

Nota

Parameter ini A adalah bagian dari sintaks pemformatan. Karena hanya ada satu nilai bidang di setiap sel, nilai ini hanya dapat diatur ke 0.

Titik dua dan nilai setelah titik dua bersifat opsional. Karakter setelah titik dua (B dalam contoh umum) menentukan format untuk menampilkan nilai. Tabel berikut mencantumkan format umum.

Format karakter Deskripsi
C Menampilkan nilai numerik dalam format mata uang.
D Menampilkan nilai numerik dalam format desimal.
E Menampilkan nilai numerik dalam format ilmiah (eksponensial).
F Menampilkan nilai numerik dalam format tetap.
G Menampilkan nilai numerik dalam format umum.
N Menampilkan nilai numerik dalam format angka.
X Menampilkan nilai numerik dalam format heksadesimal.

Nota

Kecuali untuk X, karakter format tidak peka huruf besar/kecil. Karakter X format menampilkan karakter heksadesimal dalam kasus yang ditentukan.

Nilai setelah karakter format (xx dalam contoh umum) menentukan jumlah digit signifikan atau tempat desimal yang akan ditampilkan. Misalnya, string "{0:F2}" format menampilkan angka titik tetap dengan dua tempat desimal.

Untuk informasi selengkapnya tentang pemformatan string, lihat Tipe Pemformatan.

Nilai properti ini disimpan dalam status tampilan.

Berlaku untuk

Lihat juga