Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two textboxes on a form. I have a code for the textboxes in Page Load. What I am trying to do is to have the form to auto calculate when a user inputs some numbers in the textboxes. Here is the code I have:

C#
int a = Convert.ToInt32(TextBoxTA.Text);
        int b = Convert.ToInt32(TextBoxTL.Text);
        TextBoxTNA.Text = Convert.ToString(a - b); 


The other code I have for Dividing:

C#
int a = Convert.ToInt32(TextBoxTHUG.Text);
        TextBoxTHUGDR.Text = Convert.ToString(a / 12);


I am trying to auto calculate for the user when the user inputs some numbers. Is this possible or am I doing it wrong? Please Help!!

Page Load Code:

C#
protected void Page_Load(object sender, EventArgs e)

    {
        ButtonPrint.Attributes.Add("onclick", "window.print(); return false");
        

       SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
       con.Open();
       SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
       con2.Open();

       TextBoxINST_ID.Text = Session["inst_id"].ToString();
       SqlCommand scmd = new SqlCommand("Select INST_ID, LongName, City, State from TableCOCINST where INST_ID = '" + TextBoxINST_ID.Text + "'", con);
       SqlCommand scmd2 = new SqlCommand("Select INST_ID, FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, FTEYR from TableFTE2012 where INST_ID = '" + TextBoxINST_ID.Text + "'", con2);
       SqlDataReader dr = scmd.ExecuteReader();
       SqlDataReader dr2 = scmd2.ExecuteReader();
       if (dr.Read())
       if (dr2.Read())
       {
           TextBoxLYFTUG.Text = dr2["FT_UNDERGR"].ToString();
           TextBoxLYFTG.Text = dr2["FT_GRAD"].ToString();
           TextBoxLYTHUGDR.Text = dr2["FTE_UNDERG"].ToString();
           TextBoxLYTHGDR.Text = dr2["FTE_GRAD"].ToString();
           TextBoxLYNCCDR.Text = dr2["NON_CREDIT"].ToString();
           TextBoxLYTCNC.Text = dr2["TOTAL_FTE"].ToString();
           TextBoxLYTNFUG.Text = dr2["FCFTUHC"].ToString();
           TextBoxLYTNFG.Text = dr2["FCFTPBHC"].ToString();
           TextBoxLYTNCPUG.Text = dr2["FCPTUHC"].ToString();
           TextBoxLYTNCPG.Text = dr2["FCPTPBHC"].ToString();
           TextBoxLYTNNCC.Text = dr2["NCHC"].ToString();
           TextBoxINST_ID.Text = dr["INST_ID"].ToString();
           lblSchool.Text = dr["LongName"].ToString();
           lblCity.Text = dr["City"].ToString();
           lblState.Text = dr["State"].ToString();
           lblLYear.Text = dr2["FTEYR"].ToString();
           
       }
       dr.Close();
       con.Close();
       dr2.Close();
       con2.Close();


    }
Posted
Updated 24-Oct-13 2:54am
v3
Comments
Kenneth Haugland 23-Oct-13 14:44pm    
Place your code in the TextIntput or something equvialent function, that fires whenever a key is pressed.
Computer Wiz99 23-Oct-13 14:46pm    
Ok, what do you mean? I'm not clear on what you are saying?
Kenneth Haugland 23-Oct-13 14:54pm    
Dont put it inside Page_Load, but rather like this:
http://stackoverflow.com/questions/12072281/textbox-keydown-event-in-asp-net
Computer Wiz99 23-Oct-13 15:05pm    
I see the link you have posted but I don't know java.

In Page_Load, write below code :

TextBoxTA.TextChanged += new EventHandler(TextBoxTA_TextChanged);

then write TextBoxTA_TextChanged() method where you have to place your code,

C#
void TextBoxTA_TextChanged(object sender, EventArgs e)
        {
            // Place your code here

        }


Do this for both the textboxes.
Hope it helps you.
 
Share this answer
 
Comments
Computer Wiz99 24-Oct-13 8:52am    
Tanuj Rastogi, I have a problem. I used the code you gave in Page Load, but it put the red lines uder all of my other code in Page Load. Why is that? I tried to move it to the bottom of the Page Load and still the samething. Here is the code in Page Load.
If you want to handle it on client side script Javascript/Jquery can be used .

You can try something like below

C#
<script type="text/javascript">

function Addition() {
        var TA = document.getElementById("TextBoxTA").value;
        var TL = document.getElementById("TextBoxTL").value;
        if ((TA != "") && (TL != "") && (typeof TA !== "undefined") && (TL !== "undefined")) {
            var AddResult = parseInt(TA) + parseInt(TL);
            document.getElementById("TextBoxTNA").value = AddResult.toString();
        }
    }
</script></script>


Call the JS script on any of the Keyup,Textchanged, Onblur events

XML
<asp:TextBox ID="TextBoxTA" runat="server"  onblur="Addition()" ></asp:TextBox>
        <asp:TextBox ID="TextBoxTL" runat="server" onblur="Addition()"></asp:TextBox>
        <asp:TextBox ID="TextBoxTNA" runat="server"  ></asp:TextBox>


Hope this helps
 
Share this answer
 
Comments
Computer Wiz99 24-Oct-13 8:31am    
JoCodes, Thanks for the help and code you gave. I don't know javascript that good. I was look for it in C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900