How do you clear textboxes on postback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jw56578@gmail.com

    How do you clear textboxes on postback

    When a page does a post back and there is data in a textbox, that
    textbox will get automatically repopulated with that data due to the
    posting of the form. Disabling viewstate has no affected on textbox
    controls, so how do you make it so that the text box value is cleared
    out on postback?
  • Mark Rae

    #2
    Re: How do you clear textboxes on postback

    <jw56578@gmail. com> wrote in message
    news:d3e23e59.0 410040559.a4549 fb@posting.goog le.com...[color=blue]
    > When a page does a post back and there is data in a textbox, that
    > textbox will get automatically repopulated with that data due to the
    > posting of the form.[/color]

    That's correct - that's the whole point of having textboxes (and other
    user-interactive) controls on web pages :-)
    [color=blue]
    > Disabling viewstate has no affected on textbox controls, so how do you
    > make it so
    > that the text box value is cleared out on postback?[/color]

    Well, the trite answer would be to say that if a webpage contains a textbox
    where the user can enter text, but the web app behind it isn't actually
    interested in that text, why have it on the page in the first place?

    But, clearing its value on postback is easy enough. In the Page_Load event
    check whether Page.IsPostBck is true and, if it is, set the TextBox
    control's Text property to "".


    Comment

    • Scott Allen

      #3
      Re: How do you clear textboxes on postback

      You can do this explicitly in the Load event:

      if(IsPostBack)
      {
      TextBoxControl. Text = String.Empty;
      }

      --
      Scott


      On 4 Oct 2004 06:59:22 -0700, jw56578@gmail.c om wrote:
      [color=blue]
      >When a page does a post back and there is data in a textbox, that
      >textbox will get automatically repopulated with that data due to the
      >posting of the form. Disabling viewstate has no affected on textbox
      >controls, so how do you make it so that the text box value is cleared
      >out on postback?[/color]

      Comment

      • Cowboy (Gregory A. Beamer) - MVP

        #4
        RE: How do you clear textboxes on postback

        Explicitly clear:

        private void ClearTextboxes( )
        {
        textbox1.Text = String.Empty;
        }

        You can also loop through all of them and clear all. Explicit coding, even
        when not necessary, makes the code clearer. In other words, I would clear
        them explicitly, even if .NET did it for me. Reasoning: They may find that
        they need to switch defaults some day (there is a 2.0 discussion on the
        ADO.NET model right now).


        ---

        Gregory A. Beamer
        MVP; MCP: +I, SE, SD, DBA

        *************** ************
        Think Outside the Box!
        *************** ************


        "jw56578@gmail. com" wrote:
        [color=blue]
        > When a page does a post back and there is data in a textbox, that
        > textbox will get automatically repopulated with that data due to the
        > posting of the form. Disabling viewstate has no affected on textbox
        > controls, so how do you make it so that the text box value is cleared
        > out on postback?
        >[/color]

        Comment

        Working...