2013-03-26 12 views
0

我基本上想要總量。 金額存儲在標籤中。我想添加標籤。基本上我想做一個額外的標籤,但我不能因爲標籤是.Text這就是String,所以當我添加它時,我得到一串添加標籤,而我想要添加標籤中存儲的數字。這是我的代碼如下。如何添加存儲在Label.Text中的數字

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    temp4 = Int32.Parse(DropDownList3.Text); 
    temp5 = temp4 * 76; 
    Label7.Text = temp5.ToString(); 
} 

單擊按鈕時,Lablel7中的金額應該添加另一個標籤。

protected void ImageButton3_Click(object sender, ImageClickEventArgs e) 
{ 
    Label16.Text = Label7.Text+Label6.Text; 
} 

這樣就可以找到總金額。

上午還挺新的節目和所有的本身,這是我的項目的一部分,很抱歉,如果這個問題似乎是愚蠢的

回答

1
Label16.Text = (int.parse(Label7.Text)+int.parse(Label6.Text)).toString(); 

使用上面的代碼。

將您的添加轉換爲字符串數據類型。

+0

非常感謝!有效!非常感謝這樣的快速回復!真的很感激它! – user2211486 2013-03-26 12:28:58

+0

歡迎。如果有幫助,則接受答案。 – Freelancer 2013-03-26 12:29:45

2

解析這兩個標籤的Text屬性爲整數,然後做加法。

Label16.Text = (int.Parse(Label7.Text) + int.Parse(Label6.Text)).ToString(); 

它的更好,如果你可以使用int.TryParse這將節省您的異常,如果文字不是數字。

int number1; 
int number2; 
if(!int.TryParse(Label7.Text, out number1)) 
{ 
    // invalid number in Label7 
} 

if(!int.TryParse(Label6.Text, out number2)) 
{ 
    // invalid number in Label6 
} 

Label16.Text = (number1 + number2).ToString(); 
2

Label7.Text是字符串的類型,您可以添加int,因此您必須將其轉換。 Afterr all you have convert int into string

Label16.Text = (int.Parse(Label7.Text)+int.Parse(Label6.Text)).ToString(); 

嘗試重命名您的控件和var。例如lblAmount告訴你mase比label6。請閱讀關於駱駝,帕斯卡爾轉換,它將在未來幫助你。

+0

將無法​​正常工作...必須轉換字符串 – Freelancer 2013-03-26 12:22:18

+1

現在它將工作的整個解決方案。 – Freelancer 2013-03-26 12:23:40

+0

謝謝,我忘了它 – Jacek 2013-03-26 12:25:48

1

兩個解決方案:

1您可以分析每個文本轉換成的Int32,您可以添加,然後在文本用的ToString()

protected void ImageButton3_Click(object sender, ImageClickEventArgs e) 
{ 
Label16.Text = (Int32.Parse(Label17.Text) + Int32.Parse(Label6.Text)).ToString(); 
} 

2 - 轉換在每個chanching,你可以將值保存在Int32類型的私有屬性中,並使用它們。

相關問題