2017-07-18 11 views
-1

2つのテキストボックス値を計算し、3番目のテキストボックスに正常に表示しましたが、textbox1に値を入力してtextbox2に移動すると問題が発生します。 = "true" textbox1とtextbox2に対して、このAutoPostBack = "true"を削除すると、計算された値は3番目のテキストボックスに表示されません。ここで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

サイド注:これは、クライアント側のコード(Javaスクリプト)も優れています。 – user3185569

+1

ページが毎回リロードされないようにするには、自動ポストバックを削除します。次に、ボタンを使用してサーバーにポストバックします。正直なところ、これはこの操作のために過剰です。 JavaScriptは完全に2つの数値を追加することができます。このためにサーバで前後にすべてを行う必要はありません。 – David

答えて

0

はあなたがここでボタンのクリックイベントにそれらを追加することができる2つのテキストボックスの値を追加し、3番目のテキストボックスにそれを示すようになっていますコードがある

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あなたは関数を作成し、ページのロードイベントで呼び出すことができます –

関連する問題