文本框格式仅在第三个文本框文本更改后发生。为什么? [英] Textbox Format only happens after the third textbox text change. Why?

查看:82
本文介绍了文本框格式仅在第三个文本框文本更改后发生。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单,上面有四个文本框。我在三个文本框中也有三个RangeValidator。当用户在textbox1和textbox2中输入数字时,RangeValidator将触发两个文本框,并且文本框不会格式化,直到用户选中textbox3为止。也是第一个我只想找出原因?我做错了什么?



Textbox1背后有这个代码和属性:

I have a web form that has four textboxes on it. I also have three RangeValidators on the three of the textboxes. When the user enters numbers in textbox1 and textbox2 the RangeValidator fires for both textboxes and the textbox does not format until the user tabs to textbox3. Also the first I am just trying to find out why? What did I do wrong?

Textbox1 has this code behind and properties:

<asp:TextBox ID="TextBox1" runat="server" Width="180px" AutoPostBack="True" CausesValidation="True">0</asp:TextBox>




TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));





Textbox2背后有这个代码和属性:



Textbox2 has this code behind and properties:

<asp:TextBox ID="TextBox2" runat="server" Width="180px" AutoPostBack="True" CausesValidation="True">0</asp:TextBox>




TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));





Textbox3背后有此代码属性:



Textbox3 has this code behind and properties:

<asp:TextBox ID="TextBox3" runat="server" Width="180px" AutoPostBack="True" ontextchanged="TextBox3_TextChanged">0</asp:TextBox>




TextBox3.Text = string.Format("{0:0,0}", double.Parse(TextBox3.Text));




protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
        int j = 12;
        TextBox4.Text = Convert.ToString(i / j);
        RangeValidatorLY4.Validate();
        TextBox5.Focus();
    }







Textbox4背后有这个代码和属性:




Textbox4 has this code behind and properties:

<asp:TextBox ID="TextBox4" runat="server" Width="180px" Enabled="False" ReadOnly="True" CausesValidation="True" AutoPostBack="True"></asp:TextBox>

推荐答案

如果您想要输入格式化输入文本的文本框或阻止用户输入任何内容,请使用AjaxToolkit中的MaskedTextBox或使用javascript格式化文本客户端。



否则,创建格式化函数,该函数接受TextBox控件并格式化其文本。您可以在TextChanged事件中使用它,但您必须为每个文本框创建事件处理程序。



在您的页面中

If you want to have textboxes which format the text entered or prevent users from entering just anything, use MaskedTextBox from AjaxToolkit or use javascript to format the text client side.

Otherwise, create formatting function that takes TextBox control and formats its text. You can use it in TextChanged event, but you have to create event handler for each textbox.

In your page
<asp:textbox id="TextBox1" runat="server" width="180px" autopostback="True" xmlns:asp="#unknown">
ontextchanged="TextBoxChanged_Handler">0</asp:textbox>

<asp:textbox id="TextBox2" runat="server" width="180px" autopostback="True" ontextchanged="TextBoxChanged_Handler" xmlns:asp="#unknown">0</asp:textbox>







代码背后:




Code behind:

protected TextBoxChanged_Handler(object sender, EventArgs e) {
FormatText(sender);
}

// you could add some type checking before sending the sender too
private void FormatText(TextBox txt) {
txt.Text = string.Format("{0:0,0}", double.Parse(txt));
}







您应该将控件重命名为合理的。



如果这有帮助,请花些时间接受解决方案。谢谢。




You should rename your controls to something sensible.

If this helps, please take time to accept the solution. Thank you.


这篇关于文本框格式仅在第三个文本框文本更改后发生。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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