如何在C#中的页面加载中做数学运算符 [英] How to do Mathematical Operators in Page Load in C#

查看:92
本文介绍了如何在C#中的页面加载中做数学运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否有办法在页面加载中执行数学运算符,当用户单击文本框并输入数字时,数学运算符在用户单击下一个文本框或按Tab键时起作用。另外,我试图在一个文本框中划分并将答案放到另一个文本框中。这可能吗?



页面加载代码:



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:

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();


   }

推荐答案

这样的工作没有实际意义并赢了不会让你获得任何好结果。与此同时,一切都已经在JavaScript中完成了。这就是你所需要的。我在问题评论中几乎解释了所有内容,请参阅。



缺少什么?只有一件事,关键的JavaScript功能可以做到这一切。这是 eval

http:/ /www.w3schools.com/jsref/jsref_eval.asp [ ^ ]。



你明白了吗?



如果你需要的话在ASP.NET中进行一些练习,更好地做其他事情。 :-)



-SA
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


这篇关于如何在C#中的页面加载中做数学运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆