2015-10-28 41 views
0

我需要你的幫助。
這裏我有aspx代碼和vb.net。 我想計算我的gridview有templatefield(下)的內容。當我點擊鏈接按鈕時,我正在使用鏈接按鈕提供帶有工具提示的「ID」。鏈接按鈕本身的內容是一些東西,它是數字,它應該是十進制或整數。

我有問題,當我想使頁腳總數。我想計算它們(鏈接按鈕處的數量),但鏈接按鈕本身具有字符串數據類型。
因此,當我得到e.Row.Cells(0).text時,它顯示「」(空格/無)。

在gridview中計算總的鏈接按鈕文本asp.net

我想,如果e.Row.Cells(0).text可以返回「量」的真實文本,這將是更多的幫助。

你能幫我解決嗎?
謝謝。

ASPX代碼

<asp:TemplateField HeaderText="Amount" ItemStyle-VerticalAlign="Top" HeaderStyle-HorizontalAlign="right"> 
<ItemTemplate> 
    <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="false" 
     Text='<%#Eval("amount", "{0:###,##0}")%>' ToolTip='<%#Eval("id")%>'> 
    </asp:LinkButton> 
</ItemTemplate> 
<ItemStyle HorizontalAlign="Right" /> 

VB.NET代碼

Protected Sub gvList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvList.RowDataBound 
    Dim j As Integer = 0 
    If Not IsNothing(e.Row.Cells(0).Text) And IsNumeric(e.Row.Cells(0).Text) Then 
     arrTotalPage(j) += Convert.ToDecimal(e.Row.Cells(0).Text) 
    End If 
End Sub 
+0

你從哪裏得到'金額' – Webruster

回答

0

我建議在你的代碼首先找到控制然後使用它的Text屬性來獲取價值例如

Protected Sub gvList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvList.RowDataBound 
    Dim j As Integer = 0 

    If e.Row.RowType == DataControlRowType.DataRow Then 

      // Find the button 
      Dim totalButton As LinkButton = DirectCast(e.Row.FindControl("lnkEdit"), LinkButton) 

      arrTotalPage(j) += Convert.ToDecimal(totalButton.Text) 

    End If 
End Sub 
+0

建議的代碼會在行號#6處拋出一個**格式的異常**。您嘗試將** text **轉換爲十進制,因爲金額通常具有數字格式,例如** $ 100,000.00 ** – Prabhat

+0

是的,我明白了。謝謝@SuprabhatBiswal。 –

+0

好..非常感謝你@ d-unit。它有很多幫助。我沒有想到使用findcontrol鏈接按鈕,因爲實際上我在gridview中有一些字段,所以我使用'e.Row.Cells(i)'作爲數組。 –