locked
TextBox Value RRS feed

  • Question

  • User-797751191 posted

    Hi

      I want if value in TextBox is 22000 then it should get converted to 22000.00

    Thanks

    Tuesday, July 2, 2019 5:17 AM

Answers

  • User665608656 posted

    Hi jsshivalik,

    According to your description, I suugest that you can change the input format by triggering the OnTextChanged event of the textbox control.

    Note that you need to set the AutoPostBack property of the textbox to true so that you can trigger the code behind method only after the input is complete.

    In the OnTextChanged event, by getting the value of textbox, you first convert it into data of double type, and then convert it into ToString("0.00") type to change the form of textbox value.

    You could refer to this link:https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

    Here is an example about this ,you could refer to the following code:

       <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
            </div>
        </form>

    code behind:

     protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(TextBox1.Text.ToString()))
                {
                    double dtext = Convert.ToDouble(TextBox1.Text);
                    string stext = dtext.ToString("0.00");
                    TextBox1.Text = stext;
    
                }
            }

    The result of this work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2019 6:31 AM