如果已存在零,如何为文本框文本更改计算零值? [英] How to calculte a zero for a textbox text change if zero is already there?

查看:53
本文介绍了如果已存在零,如何为文本框文本更改计算零值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单,上面有一对文本框。现在我在这些文本框中有一个零。



I have a web form that has a couple for textboxes on it. Right now I have a zero in these textboxes.

<asp:TextBox ID="TextBoxTNUGSC" runat="server" Width="180px" 

                    style="text-align: right" AutoPostBack="True" 

                    ontextchanged="TextBoxTNUGSC_TextChanged">0</asp:TextBox>





这是文本框文本更改:



Here is the textbox text change:

protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBoxTHUG.Text.Replace(",", ""));
        int j = 12;
        TextBoxTHUGDR.Text = Convert.ToString(i / j);

        int a = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
        int b = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
        int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
        int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));
        TextBoxT1234.Text = Convert.ToString(a + b + c + d);

        int g = Convert.ToInt32(TextBoxT1234.Text.Replace(",", ""));
        int f = Convert.ToInt32(TextBoxNCCDR.Text.Replace(",", ""));
        TextBoxTCNC.Text = Convert.ToString(g + f);



当用户进入网络表单时,他们会在文本框中看到零。为了使文本框textchange能够工作,他们必须输入另一个0,它现在将在文本框中显示00。如果用户无法输入这些文本框,如何才能触发文本框textchange?


When a user comes into the web form they will see the zeros there in the textboxes. In order for the textbox textchange to work they will have to enter another 0 that will now display 00 in the textbox. How can I get the textbox textchange to fire if the user has nothing to enter into those textboxes?

推荐答案

线索在事件名称中:TextChanged

它只能在文本发生变化时触发,并且在其他情况下没有好的方法可以触发它。



相反,请考虑处理Leave事件并在那里进行处理。当用户试图离开控件时你会离开 - 无论用户是否改变了任何东西都应该有效。
The clue is in the event name: "TextChanged"
It is intended to fire only when the text changes, and there is no good way to make it fire under other circumstances.

Instead, consider handling the Leave event and doing your processing there. You get Leave when the user tries to move out of the control - which should work whether the user changed anything or not.


根据代码提供的,你不会初始化字段时首先通过覆盖页面加载功能来加载它?这是你要找的吗?



Based on the code, provided wouldn't you initialise the fields when it is first loaded by overriding the Page Load function? Is this what you were looking for?

void OnLoad()
{
    updateText(0, 0, 0);
}

void updateText(int i, int abcd, int gf)
{
    int j = 12;
    TextBoxTHUGDR.Text = Convert.ToString(i / j);
    TextBoxT1234.Text = Convert.ToString(abcd);
    TextBoxTCNC.Text = Convert.ToString(gf);
}


protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
{
        int i = Convert.ToInt32(TextBoxTHUG.Text.Replace(",", ""));

        int a = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
        int b = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
        int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
        int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));

        int g = Convert.ToInt32(TextBoxT1234.Text.Replace(",", ""));
        int f = Convert.ToInt32(TextBoxNCCDR.Text.Replace(",", ""));

        updateText(i, a+b+c+d, g+f);

}


这篇关于如果已存在零,如何为文本框文本更改计算零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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