Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way for me to do Mathematical Operators in Page Load that when the user clicks in the textbox and puts in a number the Mathematical Operator works when the user clicks on the next textbox or presses the tab key. Plus I am trying to Divide in one textbox and put the answer into the other. Is that possible?

Page Load Code:

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

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

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

       int a = Convert.ToInt32(TextBoxNPRNA.Text);
       int b = Convert.ToInt32(TextBoxETRNA.Text);
       int c = Convert.ToInt32(TextBoxTUNA.Text);
       TextBoxTNA2.Text = Convert.ToString(a + b + c);

       int a = Convert.ToInt32(TextBoxTA.Text);
       TextBoxTNA.Text = Convert.ToString(a / 12);

      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
Comments
Ranjan.D 24-Oct-13 10:01am    
I really don't understand why did you coded everything with in the Page_Load. Obviously it will never work.
Computer Wiz99 24-Oct-13 10:07am    
I have some textbox that will display what the user has inputted last year and what it is this year. that is what is in the Page Load is what you see. I also wanted to do some Mathematical Operators in the Page Load for some other textboxes. Those textbox data will go into the database. I was trying to do the math for the user automatic when the user puts in the numbers. Is there a way of doing that using C#? Do I have to set those textboxes to AutoPostBack?
Ranjan.D 24-Oct-13 10:24am    
If you are anticipating user inputs for the below code, it wont work in Page Load. You will have run this code in say Button Click

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

int a = Convert.ToInt32(TextBoxNPRNA.Text);
int b = Convert.ToInt32(TextBoxETRNA.Text);
int c = Convert.ToInt32(TextBoxTUNA.Text);
TextBoxTNA2.Text = Convert.ToString(a + b + c);

int a = Convert.ToInt32(TextBoxTA.Text);
TextBoxTNA.Text = Convert.ToString(a / 12);
Computer Wiz99 24-Oct-13 10:26am    
Ok. Is there another way to write the code in C# to work in Page Load?
Ranjan.D 24-Oct-13 10:32am    
There is not way you can accept user inputs and process the same in Page_Load.

1 solution

Such work just makes no practical sense and won't allow you to get any good results. At the same time, everything is already done for you, in JavaScript. This is all you need. I explained nearly everything in my comment to the question, please see.

What's missing? Only one thing, the key JavaScript function which does it all. This is eval:
http://www.w3schools.com/jsref/jsref_eval.asp[^].

Are you getting the idea?

And if you just need some exercise in ASP.NET, better do something else. :-)

—SA
 
Share this answer
 

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