2017-03-26 51 views
0

我有3個文本框.. items..textfield名
1日)的數量 - tot_qty item..textfield名
2日)價格 - 價格
3)總price..textfield名 - tot_price文本已改變事件不工作

我想要的.. 當我輸入的數量字段中的特定值,然後在價格領域,相乘的值會自動出現在總量場..

我已經使用了以下代碼

protected void tot_price_TextChanged(object sender, EventArgs e) { 

    tot_price.Text = (float.Parse(tot_qty.Text) *  float.Parse(price.Text)).ToString(); 
                    } 

但是,在總量字段中輸入值然後在價格字段中輸入值然後再按壓標籤也沒有任何反應。

+0

要明白這是一個服務器端事件,它需要一個回傳。在這裏可能是可以接受的,但一般來說你應該用一些js庫/框架來看看JavaScript。 –

回答

0

我認爲你需要添加屬性AutoPostBack =「True」到文本框。

<asp:TextBox ID="tot_price" runat="server" AutoPostBack="True"></asp:TextBox> 

欲瞭解更多信息,請查看https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback(v=vs.110).aspx

編輯:

<asp:TextBox runat="server" ID="tot_qty"></asp:TextBox> 
    <asp:TextBox runat="server" ID="price" AutoPostBack="True" OnTextChanged="tot_price_TextChanged"></asp:TextBox> 
    <asp:TextBox runat="server" ID="tot_price" ></asp:TextBox> 

代碼隱藏

protected void tot_price_TextChanged(object sender, EventArgs e) 
    { 
     tot_price.Text = (float.Parse(tot_qty.Text) * float.Parse(price.Text)).ToString(); 
    } 
+0

你好,我確實嘗試並將textfield的autopostback屬性設置爲true,但無濟於事 –

+0

我編輯了我的答案以包含使用的代碼,我剛剛嘗試過。如果它仍然無法正常工作,請發佈您的代碼進行檢查。 –

+0

你可以在你的'tot_price_TextChanged'方法上設置一個斷點,附上調試器/按下F5,看看你的事件是否被擊中? – sh1rts

-1

如果按 '選項卡',您可以添加 '離開' 事件(如它的勝利形式的應用程序)

private void textBox1_Leave(object sender, EventArgs e) 
    { 
     ot_price.Text = (float.Parse(tot_qty.Text) * float.Parse(price.Text)).ToString(); 
    } 
+0

這不是一個WinForms應用程序 – sh1rts