2012-04-02 54 views
1

如何通過ASP.NET GridView onClientClick標記引用當前行中的字段。GridView OnClientClick標記中的引用字段

下面的作品,但是,我已經硬編碼文本「條形碼」中的String.Format

<asp:GridView ID="ItemsScannedGridView" runat="server" 
     AutoGenerateColumns="False" DataKeyNames="ID" 
     DataSourceID="SqlDataSourceItems" 
     CssClass="ScannedBarcodes"> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" OnClientClick=<%# String.Format("return confirm('Are you sure you wish to delete barcode {0}?');", "barcode") %> /> 
       </ItemTemplate> 
      </asp:TemplateField> 

      <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
       ReadOnly="True" SortExpression="ID" Visible="False" /> 
      <asp:ImageField DataAlternateTextField="MATCH" 
       DataAlternateTextFormatString="{0}" DataImageUrlField="MATCH" 
       DataImageUrlFormatString="~/images/icons/{0}.png" HeaderText="Match"> 
      </asp:ImageField> 
      <asp:BoundField DataField="BARCODE" HeaderText="Barcode" 
       SortExpression="BARCODE" Visible="False" /> 
      <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" 
       SortExpression="DESCRIPTION" /> 
     </Columns> 
    </asp:GridView> 

<%# String.Format("return confirm('Are you sure you wish to delete barcode {0}?');", "barcode") %> />

我試着用Eval("barcode")取代「條形碼」,但沒有奏效。

回答

1
<asp:Button ID="DeleteButton" 
     runat="server" 
     CommandName="Delete" 
     Text="Delete" 
     OnClientClick=<%# String.Format("return confirm('Are you sure you wish to delete barcode {0}?');", Eval("BARCODE")) %> /> 
+0

這是不行的,網格行完全不 – jax 2012-04-03 03:20:04

+0

顯示在調試我得到這個,類型「System.Web.HttpException」的異常出現在System.Web.dll中,但用戶沒有處理代碼 附加信息:DataBinding:'System.Data.DataRowView'不包含名爲'BARCODE'的屬性。 – jax 2012-04-03 03:29:05

+0

我明白了,條形碼實際上並不是從SQL中選擇的,但是,上面的代碼必須是以前的代碼。 – jax 2012-04-03 03:34:29