ButtonField.DataTextFormatString プロパティ

定義

フィールドの値の表示形式を指定する文字列を取得または設定します。

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

プロパティ値

フィールドの値の表示形式を指定する書式指定文字列。 既定値は空の文字列 ("") です。この値は、フィールド値に適用される特定の書式が設定されていないことを示します。

次のコード例では、 プロパティを DataTextFormatString 使用して、フィールドの値のカスタム表示形式を指定する方法を示します。


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

注釈

オブジェクトに DataTextFormatString 表示される値のカスタム表示形式を指定するには、 プロパティを ButtonField 使用します。 プロパティが DataTextFormatString 設定されていない場合、フィールドの値は特別な書式なしで表示されます。

注意

書式指定文字列は、 プロパティが設定されている場合 DataTextField にのみ適用されます。

書式指定文字列には任意のリテラル文字列を指定でき、通常はフィールドの値のプレースホルダーが含まれます。 たとえば、書式指定文字列 "Item Value: {0}"では、 {0} プレースホルダーは オブジェクトに表示されるときにフィールドの値に ButtonField 置き換えられます。 書式指定文字列の残りの部分は、リテラル テキストとして表示されます。

注意

書式指定文字列にプレースホルダーが含まれていない場合、データ ソースのフィールドの値は最終的な表示テキストには含まれません。

プレースホルダーは、コロンで区切られ、中かっこで囲まれた 2 つの部分で構成されます({ A : Bxx })。 コロンの前の値 (A 一般的な例では) は、パラメーターの 0 から始まるリスト内のフィールド値のインデックスを指定します。

注意

この A パラメーターは書式設定構文の一部です。 各セルにはフィールド値が 1 つしかないため、この値は 0 にのみ設定できます。

コロンとコロンの後の値は省略可能です。 コロンの後の文字 (B 一般的な例では) は、値を表示する形式を指定します。 次の表に、一般的な形式を示します。

文字の書式設定 説明
C 数値を通貨形式で表示します。
D 数値を 10 進形式で表示します。
E 数値を指数 (指数) 形式で表示します。
F 数値を固定形式で表示します。
G 数値を一般的な形式で表示します。
N 数値を数値形式で表示します。
X 数値を 16 進数形式で表示します。

注意

X除き、書式文字では大文字と小文字は区別されません。 書式文字には X 、指定した場合の 16 進文字が表示されます。

書式文字 (xx の一般的な例では) の後の値は、表示する有効桁数または小数点以下の桁数を指定します。 たとえば、書式指定文字列 "{0:F2}" には、小数点以下 2 桁の固定小数点番号が表示されます。

書式指定文字列の詳細については、「Formatting Types」(型の書式設定) を参照してください。

このプロパティの値はビュー ステートに格納されます。

適用対象

こちらもご覧ください