如何自动计算页面加载 [英] How to Auto Calculate on Page Load
问题描述
我在表单上有两个文本框.我有一个页面加载中的文本框代码.我想做的是让表单在用户在文本框中输入一些数字时自动计算.这是我的代码:
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:
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:
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:
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();
}
推荐答案
在Page_Load中,编写以下代码:
TextBoxTA.TextChanged + =新的EventHandler(TextBoxTA_TextChanged);
然后在必须放置代码的地方编写TextBoxTA_TextChanged()方法,
In Page_Load, write below code :
TextBoxTA.TextChanged += new EventHandler(TextBoxTA_TextChanged);
then write TextBoxTA_TextChanged() method where you have to place your code,
void TextBoxTA_TextChanged(object sender, EventArgs e)
{
// Place your code here
}
对两个文本框都执行此操作.
希望对您有帮助.
Do this for both the textboxes.
Hope it helps you.
如果要在客户端脚本上处理它,可以使用Javascript/Jquery.
您可以尝试以下
If you want to handle it on client side script Javascript/Jquery can be used .
You can try something like below
<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>
在任何Keyup,Textchanged,Onblur事件中调用JS脚本
Call the JS script on any of the Keyup,Textchanged, Onblur events
<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
这篇关于如何自动计算页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!