Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way for a user to enter data into a textbox and then tab to the next textbox for it to auto calculate the numbers. If a user enters 25 in textbox1 and the math formula is */5 =, then in textbox2 the answer will be 5. Is there a way of doing this in C#?

C#
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;
using System.Web.SessionState;

public partial class FinancialProfileFormD : System.Web.UI.Page
{
    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, INSTRUCTIO, RESEARCH, ACADEMIC_S, NET_AID, AUXILIARY_, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT from TableFIN2012 where INST_ID = '" + TextBoxINST_ID.Text + "'", con2);
       SqlDataReader dr = scmd.ExecuteReader();
       SqlDataReader dr2 = scmd2.ExecuteReader();
        if (dr.Read())
        if (dr2.Read())
            {
            lblSchool2.Text = dr["LongName"].ToString();
            lblSchool.Text = dr["LongName"].ToString();
            lblCity.Text = dr["City"].ToString();
            lblState.Text = dr["State"].ToString();
            TextBoxLYInstr.Text = dr2["INSTRUCTIO"].ToString();
            TextBoxLYResPs.Text = dr2["RESEARCH"].ToString();
            TextBoxLYAcadSSSIS.Text = dr2["ACADEMIC_S"].ToString();
            TextBoxLYAuxE.Text = dr2["AUXILIARY_"].ToString();
            TextBoxLYNGAS.Text = dr2["NET_AID"].ToString();
            TextBoxLYAOE.Text = dr2["OTHEREXP"].ToString();
            TextBoxLYTA.Text = dr2["TOTASSETS"].ToString();
            TextBoxLYTL.Text = dr2["TOTLIABILITY"].ToString();
            TextBoxLYNPRNA.Text = dr2["NoNEXPPERMRESASSETS"].ToString();
            TextBoxLYTUNA.Text = dr2["UNRNETASSETS"].ToString();
            TextBoxLYTR.Text = dr2["TOTALREV"].ToString();
            TextBoxLYTFN.Text = dr2["TUITFEES"].ToString();
            TextBoxLYCD.Text = dr2["CURRDEBT"].ToString();
            TextBoxLYLTD.Text = dr2["LONGTERMDEBT"].ToString();
            
        }
        dr.Close();
        con.Close();
        dr2.Close();
        con2.Close();
    }
    
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);


        SqlCommand cmd = new SqlCommand ("Insert into TableFIN2013 (INST_ID, TOTAL_REVE, INSTRUCTIO, RESEARCH, ACADEMIC_S, NET_AID, AUXILIARY_, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LOMGTERMDEBT) values (@INST_ID, @TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @ACADEMIC_S, @NET_AID, @AUXILIARY_, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LOMGTERMDEBT)", con);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID);
        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTR);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxResPs.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcadSSSIS.Text);
        cmd.Parameters.AddWithValue("@NET_AID", TextBoxNGAS.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxAOE.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
        cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
        cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
        cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
        cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
        cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
        cmd.Parameters.AddWithValue("@LOMGTERMDEBT", TextBoxLTD.Text);

        
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Redirect("FinancialProfileFormD.aspx");
        

    }
    protected void ButtonPrint_Click(object sender, EventArgs e)
    {

    }
    
   }
Posted
Comments
Tom Marvolo Riddle 29-Oct-13 9:14am    
Use textchanged event to do this
Computer Wiz99 29-Oct-13 9:15am    
Ok. Can you give me an example?

1 solution

Set Textbox1 Autopostback=true

C#
protected void textbox1_TextChanged(object sender, EventArgs e)
 {

   int i=Convert.ToInt32(textbox1.Text);
   int j=5;
   TextBox2.Text =Convert.ToString( i / j);

 }
 
Share this answer
 
Comments
Computer Wiz99 29-Oct-13 9:26am    
Jas24, Ok, I got the code. Can I put it in the Page_Load or just before the Submit Button event?
Tom Marvolo Riddle 29-Oct-13 9:28am    
Put it before submit button event

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