2012-06-07 90 views
0

我一直在使用asp.net web窗體和控件進行練習,現在已經陷入了在用戶輸入表單中處理數字數據的最佳方式,實例一個簡單的例子是一個名爲'length'的輸入字段,一個名爲width的用戶點擊一個按鈕,計算並顯示L * W的結果。使用ASP.NET web窗體控件中的數值計算

我已經測試將字符串轉換爲int使用Convert.ToInt32(strvalue2);並把結果輸出到控制檯上。

但是當我嘗試使用這樣的事情他們並輸出其轉換爲一個表單控件:

 string strvaluel = length.Text; 
     string strvalueW = width.Text; 


      int numValuel = Convert.ToInt32(strvaluel); 
      int numValueW = Convert.ToInt32(strvalueW); 
      outputbox.Text = numValuel * numValueW; (error cannot explicitly convert type 'int' to 'string' 

我很新的ASP.NET所以任何幫助,將不勝感激

+1

在一個側面說明,你應該使用'int.TryParse()'是由用戶提供的字符串時,將字符串轉換爲整數,這樣就可以適當地處理他們輸入非整數值的情況。 – Servy

+0

@Servy感謝您的建議... – Ledgemonkey

回答

6

轉換它把一個字符串:

outputbox.Text = (numValuel * numValueW).ToString(); 

如果你想對格式(逗號等)更多的控制使用格式字符串:

outputbox.Text = string.Format("{0:N}",numValuel * numValueW); 

documentation on format strings更多細節

+0

@D Stanley so .ToString();在計算之後將int的內容轉換回字符串?非常感謝 – Ledgemonkey

+0

@D斯坦利測試和工作很好,非常感謝您的幫助 – Ledgemonkey

+0

@Ledgemoneky計算的結果被轉換,而不是參數。它相當於'int i = numValuel * numValueW; outputbox.Text = i.ToString();'。如果這就是你要求的,原始值是不變的。 –

相關問題