2017-07-18 132 views
-1

我已經計算出兩個文本框的值並將其成功顯示到第三個文本框中,但問題是當我輸入值到textbox1並移動到textbox2頁面重新加載時,我已經提到過AutoPostBack =「true」爲textbox1和textbox2,當我刪除此AutoPostBack =「true」時,計算值不會顯示在第三個文本框中。顯示第2個文本框的計算值到第3個文本框

這裏是我的身後

protected void txttotal_TextChanged(object sender, EventArgs e) 
     { 
    if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text)) 
     txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString(); 
} 

protected void txtdiscount_TextChanged(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text)) 
     txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString(); 
} 

下面的代碼是ASPX代碼

<asp:TextBox ID="txttotal" runat="server" CssClass="textstyle" OnTextChanged="txttotal_TextChanged" AutoPostBack="true"></asp:TextBox> 
    <asp:TextBox ID="txtdiscount" runat="server" CssClass="textstyle" OnTextChanged="txtdiscount_TextChanged" AutoPostBack="true"></asp:TextBox> 
    <asp:TextBox ID="txtgrandtotal" runat="server" CssClass="textstyle" ReadOnly="true"></asp:TextBox> 
+2

側面說明:這是最好是一個客戶端代碼(JavaScript)的。 – user3185569

+1

如果您不希望每次都重新加載頁面,請刪除自動回發。然後使用一個按鈕回發到服務器。雖然說實話這對這個操作來說是過分的。 JavaScript完全可以添加兩個數字,你不需要爲此與服務器進行所有的這種反覆操作。 – David

回答

0

要添加兩個文本框的值,並顯示在第三個文本框,你可以將它們添加按鈕點擊事件在這裏是代碼

aspx頁面

<div> 
    <asp:TextBox runat="server" ID="txt1"></asp:TextBox> 
    <asp:TextBox runat="server" ID="txt2"></asp:TextBox> 
     <br /> 
     <asp:Label runat="server" Text="Answer"></asp:Label> <asp:TextBox runat="server" ID="txt3"></asp:TextBox> 
     <asp:Button runat="server" ID="btn1" Text="CLICK TO ADD" OnClick="btn1_Click"/> 
    </div> 

的.cs

protected void btn1_Click(object sender, EventArgs e) 
    { 
     if (!string.IsNullOrEmpty(txt1.Text) && !string.IsNullOrEmpty(txt2.Text)) 
     { 

      txt3.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text)).ToString(); 
     } 
    else { 

     Response.Write("Please Enter Value"); 
    } 
} 

enter image description here

+0

我不想讓它按鈕點擊謝謝 – nirmala

+0

@nirmala你可以創建一個函數並在頁面加載事件中調用它 –

相關問題