locked
Textbox auto populate Number when user entered RRS feed

  • Question

  • Hi All,

    I have one Requirement..can any one suggest me how to approach...

    I have one Text box, when user enters numbers into text box it will automatically format like this 66-3333333

    When page loads textbox is empty.

    When user tries to enter data into Text box it will populate automatically in the below format.

    Can any one suggest me...

    Thanks in Advance...

    Friday, November 28, 2014 7:50 AM

Answers

  • No don't call the method in page_load

    protected void change_txt(object sender, EventArgs e)
        {
            string texttoformat = txtField.Text.ToString();

            string result = texttoformat.Insert(2, "-");

            txtField.Text = result;
        }

    In Design:

    <asp:TextBox ID="txtField" AutoPostBack="true" OnTextChanged="change_txt" runat="server"></asp:TextBox>

    That's it !!!

    • Proposed as answer by Ismaiel AKKA Friday, November 28, 2014 10:15 AM
    • Marked as answer by Reddy416 Friday, November 28, 2014 11:27 AM
    Friday, November 28, 2014 10:15 AM

All replies

  • PLEASE give me more détails :

    always 66-333333 OR WHEN and what's mean this number ?

    and when it appears so user can't write anything in this textbox?

    Friday, November 28, 2014 8:12 AM
  • Hi,

    You can write one list item event receiver.

    http://onceinawhilescribble.blogspot.in/2013/05/creating-simple-event-receiver-in.html

    in item added event  read the number and format it according to your requirement.

    Friday, November 28, 2014 8:16 AM
  • HI Ismaiel,

    Not always 66-333333 .I said for example.

    When user enters values into text box it will display automatically in this format i.e.xx-xxxxxxx (if he enter any value i need that format)

    Thanks....

    Friday, November 28, 2014 9:03 AM
  • Hello,

    You may try with jquery to set focus on textbox field and replace the value.

    http://kyokasuigetsu25.wordpress.com/2011/06/13/formatting-numbers-in-jquery/

    Hope it could help


    Hemendra:Yesterday is just a memory,Tomorrow we may never see<br/> Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Friday, November 28, 2014 9:07 AM
  • Hi Agrawal,

    I Tried as per our reference site.It's not working.

    In My requirement is ,when user enter values into textbox..it will automatically format like this xx-xxxxxxx

    Thanks,

    Friday, November 28, 2014 9:27 AM
  • hi,

    with textbox control you have the method "Onfocus" when you put it you can fire un event that does whetever you want in this method fired you can set a value in textbox and you can choose the format that you want :

    <asp:TextBox runat="server" id="textboxTest" onfocus="SetTextInTextBox2()" Autopostback="true"/>

    also you can use gotfocus or enter and don't forget to active the autopostback

    Hope it helps

    • Proposed as answer by Ismaiel AKKA Friday, November 28, 2014 9:29 AM
    Friday, November 28, 2014 9:29 AM
  • Hi Ismaiel,

    In My requirement is ,when user enter values into textbox..it will automatically format like this xx-xxxxxxx

    Thanks,

    Friday, November 28, 2014 9:34 AM
  • hi,

    ok you can use then ontextchanged for textbox

    <asp:TextBox runat="server" id="textboxTest" ontextchanged="SetTextInTextBox2()" Autopostback="true"/>

    and for the method : SetTextInTextBox2()

    do this :

    string texttoformat = textboxTest.Text.toString();

    string result = texttoformat .Insert(2, "-");

    textboxTest.Text=result;

    That's All

    Hope resolved now

    • Proposed as answer by Ismaiel AKKA Friday, November 28, 2014 9:51 AM
    Friday, November 28, 2014 9:49 AM
  • Hi,

    I Tried below like this ...it's getting error..Method name expected

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetTextInTextBox2();
            }


        }--->here i am calling method

    public void SetTextInTextBox2()
        {
            string texttoformat = txtField.Text.ToString();

            string result = texttoformat.Insert(2, "-");

            txtField.Text = result;
        }---->this the method

    In Design:

    <asp:TextBox ID="txtField" runat="server" ontextchanged="SetTextInTextBox2()"></asp:TextBox>

    Under button click:

    item["FEID"] = txtField.Text;

    Getting error...

    Thanks...

    Friday, November 28, 2014 10:06 AM
  • No don't call the method in page_load

    protected void change_txt(object sender, EventArgs e)
        {
            string texttoformat = txtField.Text.ToString();

            string result = texttoformat.Insert(2, "-");

            txtField.Text = result;
        }

    In Design:

    <asp:TextBox ID="txtField" AutoPostBack="true" OnTextChanged="change_txt" runat="server"></asp:TextBox>

    That's it !!!

    • Proposed as answer by Ismaiel AKKA Friday, November 28, 2014 10:15 AM
    • Marked as answer by Reddy416 Friday, November 28, 2014 11:27 AM
    Friday, November 28, 2014 10:15 AM
  • Hi Ismaiel,

    Thanks for your help...It's working fne

    But i have one doubt...We set autopostback=true then only it will fomat text box data i.e.xx-xxxxxxx

    When it's post back my page data will be lost.Again i have to enter from starting..

    Can you plz suggest me....

    Friday, November 28, 2014 11:01 AM
  • Not at all Please put the answer as correct and the question as answered thanks,

    For Autopostback in textbox it is only for textbox not for the whole page and if you want to conserve it you have to put the code in page load and you have to put a default text because you can't tell the page that you will reload and conserve my data

    understand?

    Thanks

    Friday, November 28, 2014 11:06 AM