更新时更新文本框而不刷新 [英] update textbox when changed without refreshing
问题描述
我正在开发自己的aspx程序,现在我有一个计算器,我希望能够填写一些值,并且当您按Tab键到下一个值并填充它时,它会自动计算并将其写入一个文本框(或标签)。这必须不必刷新页面。
可以与c#结合使用吗?或者我需要寻找php + ajax来完成这项工作?
我有以下代码: p>
< asp:TextBox ID =tbx_rmsonchanged =textboxcalcrunat =server>< / asp:TextBox> ;
< asp:TextBox ID =tbx_voltonchanged =textboxcalcrunat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_anw1Enabled =falserunat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_anw2Enabled =falserunat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_effonchanged =textboxcalcrunat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_anw3Enabled =falserunat =server>< / asp:TextBox>
这是c#代码:
public void textboxcalc(object sender,EventArgs e)
{
if(tbx_rms.Text!=&& tbx_volt.Text!=)
{
double rms = Convert.ToDouble(tbx_rms.Text);
double volt = Convert.ToDouble(tbx_volt.Text);
double ant = Convert.ToDouble(rms / volt);
if(tbx_eff.Text!=)
{
double effi = Convert.ToDouble(tbx_eff.Text);
double tot = Convert.ToDouble((effi / ant)* 100);
tbx_anw3.Text = Convert.ToString(tot);
}
tbx_anw1.Text = Convert.ToString(ant);
tbx_anw2.Text = Convert.ToString(ant);
}
我希望这足够清楚我的意图,我不会很高兴地回答任何问题。
听完评论之后,我继续前进,并使用JavaScript进行计算。
< script type =text / javascript>
函数calctxtboxes(){
var rms = document.getElementById('<%= tbx_rms.ClientID%>').value;
var volt = document.getElementById('<%= tbx_volt.ClientID%>')。value;
var effi = document.getElementById('<%= tbx_eff.ClientID%>')。value;
if(rms!=&& volt!=){
var ant = rms / volt;
document.getElementById('tbx_anw1')。value = ant;
document.getElementById('tbx_anw2')。value = ant;
if(effi!=){
var total =(ant / effi)* 100;
document.getElementById('tbx_anw3')。value = total;
}
}
}
< / script>
以及像这样的aspx:
< asp:TextBox ID =tbx_rmsonchange =calctxtboxes()runat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_voltonchange =calctxtboxes()runat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_anw1Enabled =falserunat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_anw2Enabled =falserunat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_effonchange =calctxtboxes()runat =server>< / asp:TextBox>
< asp:TextBox ID =tbx_anw3Enabled =falserunat =server>< / asp:TextBox>
I'm working on my own aspx program and now i have a calculator which i want to be able to fill in some values and when you press tab to the next value and fill it in it will automatically calculate and write it into a textbox(or label). this must be without having to refresh the page. would this be possible in combination with c# or would i need to look for php + ajax to get this done?
i had the following coded:
<asp:TextBox ID="tbx_rms" onchanged="textboxcalc" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_volt" onchanged="textboxcalc" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_anw1" Enabled="false" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_anw2" Enabled="false" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_eff" onchanged="textboxcalc" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_anw3" Enabled="false" runat="server"></asp:TextBox>
and this is the c# code:
public void textboxcalc(object sender, EventArgs e)
{
if (tbx_rms.Text != "" && tbx_volt.Text != "")
{
double rms = Convert.ToDouble(tbx_rms.Text);
double volt = Convert.ToDouble(tbx_volt.Text);
double ant = Convert.ToDouble(rms / volt);
if (tbx_eff.Text != "")
{
double effi = Convert.ToDouble(tbx_eff.Text);
double tot = Convert.ToDouble((effi / ant) * 100);
tbx_anw3.Text = Convert.ToString(tot);
}
tbx_anw1.Text = Convert.ToString(ant);
tbx_anw2.Text = Convert.ToString(ant);
}
I hope it is clear enough what my intentions are and if not i will happily answer any questions.
after listening to the comments i just went ahead and used javascript to make this calculation.
<script type="text/javascript">
function calctxtboxes() {
var rms = document.getElementById('<%=tbx_rms.ClientID%>').value;
var volt = document.getElementById('<%=tbx_volt.ClientID%>').value;
var effi = document.getElementById('<%=tbx_eff.ClientID%>').value;
if (rms != "" && volt !="") {
var ant = rms / volt;
document.getElementById('tbx_anw1').value = ant;
document.getElementById('tbx_anw2').value = ant;
if (effi != "") {
var total = (ant / effi)*100;
document.getElementById('tbx_anw3').value = total;
}
}
}
</script>
and the aspx like this:
<asp:TextBox ID="tbx_rms" onchange="calctxtboxes()" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_volt" onchange="calctxtboxes()" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_anw1" Enabled="false" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_anw2" Enabled="false" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_eff" onchange="calctxtboxes()" runat="server"></asp:TextBox>
<asp:TextBox ID="tbx_anw3" Enabled="false" runat="server"></asp:TextBox>
这篇关于更新时更新文本框而不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!