2013-03-29 27 views
-1

我寫過這段代碼將每個文本框的值綁定到標籤上,這對每個單獨的值都有效,但現在我希望標籤綁定每個數值次數意味着使用for循環爲此,我可以綁定不同的值併爲迭代次數動態顯示它,所以它怎麼可能。如何綁定不同數量的值的標籤

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string name = TextBox1.Text; 
    string order = TextBox2.Text; 
    int qty = Convert.ToInt32(TextBox3.Text); 
    int rate = Convert.ToInt32(TextBox4.Text); 
    int discount = Convert.ToInt32(TextBox5.Text); 
    int c = (Convert.ToInt32(TextBox3.Text) * Convert.ToInt32(TextBox4.Text) - Convert.ToInt32(TextBox5.Text)); 
    TextBox6.Text = c.ToString(); 
    int total = Convert.ToInt32(TextBox6.Text); 
    string j=""; 
    for (int i = 1; i < 5; i++) 
    { 
     j += i + ""; 

     Label1.Text = j + "customer name is:-" + name; 
     Label2.Text = j + "order item is:-" + order; 
     Label3.Text = j + "quantity is:-" + qty; 
     Label4.Text = j + "rate of the item is:-" + rate; 
     Label5.Text = j + "discount given to the item is:-" + discount; 
     Label6.Text = j + "Total of the item is:-" + total; 
    } 

    TextBox1.Text = string.Empty; 
    TextBox2.Text = string.Empty; 
    TextBox3.Text = string.Empty; 
    TextBox4.Text = string.Empty; 
    TextBox5.Text = string.Empty; 
    TextBox6.Text = string.Empty; 
} 
+3

您可以回來編輯您的問題 - 將其分解爲連貫的句子,並使用正常的句子框,創建適當的標題等。這會讓你更容易理解你的問題。 – slugster

回答

0

在你的循環中,你覆蓋了每個標籤的值......所以最後你只剩下最後的值。

你想要做的就是標籤的值設置爲它前值什麼新值

Label1.Text += j + "customer name is:-" + name; 

+=是一樣的:

Label1.Text = Label1.Text + j + "customer name is:-" + name; 

爲了便於閱讀,您可能需要更類似的東西:

for (int i = 1; i < 5; i++) 
{ 
    Label1.Text += i + " customer name is:-" + name + "<br />"; 
    Label2.Text += i + " order item is:-" + order + "<br />"; 
    Label3.Text += i + " quantity is:-" + qty + "<br />"; 
    Label4.Text += i + " rate of the item is:-" + rate + "<br />"; 
    Label5.Text += i + " discount given to the item is:-" + discount + "<br />"; 
    Label6.Text += i + " Total of the item is:-" + total + "<br />"; 
} 

而你需要更多的錯誤檢查如Convert.ToInt32(TextBox3.Text)。如果轉換失敗,Convert.ToInt32會引發異常。

如果進入大循環,您可能需要考慮諸如StringBuilder之類的東西。

+0

但這個代碼適用於單個值,我想循環不同數量的值。 – narendra

+0

你需要澄清你的問題。你在談論什麼其他的價值觀?你試過這個代碼嗎? – MikeSmithDev

+0

假設我輸入一個名稱=麥克,訂單=披薩,數量= 4,費率= 200降壓,折扣= 10%,總= smthin。 – narendra