Clearing child control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fosterb
    New Member
    • Dec 2008
    • 8

    Clearing child control

    I am a c# developer struggling with an ASP.NET problem and I have no web development experience at all, so I suspect that my question will be an easy one to the right person.

    I am trying to fix a bug whereby a licence number is not being cleared from a web page. The user clicks an "Add" button on a web page, gets directed to a new page, enters their licence number which is made up of 5 text boxes in a line, submits the page and goes back to the first one. If they click the add button again, the previous licence number is still displayed.

    I said I would have a look into it as there are currently no asp.net developers in the company any more. I can see a class for the LicenceNumberCo ntrol, which derives from FieldControl. It has five string class variables for the 5 sections of the licence number and a get and set property. When I debug the code, the 5 class variables are null but somehow the form still manages to show the values. I think it is something to do with ChildControls which I don't understand at all.

    This is the code for the LicenceNumber property. The setter definately gets called at the right point with a null value to clear the data, please can you let me know if there is something else that should get cleared by the setter which currently isn't.

    Code:
    get{
    EnsureChildControls();
        TextBox part1 = (TextBox)this._container.FindControl("Part1");
        TextBox part2 = (TextBox)this._container.FindControl("Part2");
        TextBox part3 = (TextBox)this._container.FindControl("Part3");
        TextBox part4 = (TextBox)this._container.FindControl("Part4");
        TextBox part5 = (TextBox)this._container.FindControl("Part5");
        return part1.Text + "-" + part2.Text + "-" + part3.Text + "-" + part4.Text + "-" + part5.Text;
    }
    set{
        if (string.IsNullOrEmpty(value)){
            this._part1 = "";
            this._part2 = "";
            this._part3 = "";
            this._part4 = "";
            this._part5 = "";
        }
        else if (ValidateLicenceString(value)){
            this._part1 = value.Substring(0, 3);
            this._part2 = value.Substring(4, 6);
            this._part3 = value.Substring(11, 1);
            this._part4 = value.Substring(13, 6);
            this._part5 = value.Substring(20, 3);
    
            if (ChildControlsCreated){
                TextBox part1 = (TextBox)this._container.FindControl("Part1");
                TextBox part2 = (TextBox)this._container.FindControl("Part2");
                TextBox part3 = (TextBox)this._container.FindControl("Part3");
                TextBox part4 = (TextBox)this._container.FindControl("Part4");
                TextBox part5 = (TextBox)this._container.FindControl("Part5");
                part1.Text = this._part1;
                part2.Text = this._part2;
                part3.Text = this._part3;
                part4.Text = this._part4;
                part5.Text = this._part5;
            }
        }
    }
    Thanks,

    Ben.
    Last edited by Curtis Rutland; Dec 3 '08, 02:49 PM. Reason: Added Code Tags - Please surround your code with [CODE] and [/CODE]
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by fosterb
    I am trying to fix a bug whereby a licence number is not being cleared from a web page. The user clicks an "Add" button on a web page, gets directed to a new page, enters their licence number which is made up of 5 text boxes in a line, submits the page and goes back to the first one. If they click the add button again, the previous licence number is still displayed.
    When ViewState is enabled on a control it remembers it's value between postbacks. It seems like your TextBoxes are retaining their ViewState...

    This can only happen if the user remains on the page that they are working with. If you use Response.Redire ct() to redirect the user between the pages then this is probably not the problem....howe ver, if your "license page" is actually a component of the main page then this could be the source of your problem.

    -Frinny

    Comment

    • fosterb
      New Member
      • Dec 2008
      • 8

      #3
      Excellent, thanks for that information. I added an attribute of:
      EnableViewState ="false"
      to the control on the aspx page and this seems to fix the problem for me. I am still a little confused though, as I don't think that we are staying on the same page, I can't see a Response.Redire ct anywhere but the two pages have different .aspx files that describe them. I guess I have a lot of asp.net learning to do. Presumably it is something to do with how you define a page, being different to how I define a page.

      Many thanks for the quick response anyway!!!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by fosterb
        Excellent, thanks for that information. I added an attribute of:
        EnableViewState ="false"
        to the control on the aspx page and this seems to fix the problem for me. I am still a little confused though, as I don't think that we are staying on the same page, I can't see a Response.Redire ct anywhere but the two pages have different .aspx files that describe them. I guess I have a lot of asp.net learning to do. Presumably it is something to do with how you define a page, being different to how I define a page.

        Many thanks for the quick response anyway!!!
        ASPX would be a new "page".
        An ASCX file will be a user control that you would use in the ASPX page.

        You may not be using Response.Redire ct. You may be using Server.Transfer () to change between pages.

        I this case it makes a lot of sense as to why your page is retaining it's values....when you use Server.Transfer () to switch between pages all of the data from the previous page is retained. So...if you start on page 1, go to page 2 for the licenses, go back to page 1 the licenses are remembered...no w when you go back to page 2...they are still remembered.

        I recommend you investigate the differences between Server.Transfer and Response.Redire ct.

        -Frinny

        Comment

        • fosterb
          New Member
          • Dec 2008
          • 8

          #5
          Will do.

          Thanks again.

          Comment

          Working...