473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 LicenceNumberControl, 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.

Expand|Select|Wrap|Line Numbers
  1. get{
  2. EnsureChildControls();
  3.     TextBox part1 = (TextBox)this._container.FindControl("Part1");
  4.     TextBox part2 = (TextBox)this._container.FindControl("Part2");
  5.     TextBox part3 = (TextBox)this._container.FindControl("Part3");
  6.     TextBox part4 = (TextBox)this._container.FindControl("Part4");
  7.     TextBox part5 = (TextBox)this._container.FindControl("Part5");
  8.     return part1.Text + "-" + part2.Text + "-" + part3.Text + "-" + part4.Text + "-" + part5.Text;
  9. }
  10. set{
  11.     if (string.IsNullOrEmpty(value)){
  12.         this._part1 = "";
  13.         this._part2 = "";
  14.         this._part3 = "";
  15.         this._part4 = "";
  16.         this._part5 = "";
  17.     }
  18.     else if (ValidateLicenceString(value)){
  19.         this._part1 = value.Substring(0, 3);
  20.         this._part2 = value.Substring(4, 6);
  21.         this._part3 = value.Substring(11, 1);
  22.         this._part4 = value.Substring(13, 6);
  23.         this._part5 = value.Substring(20, 3);
  24.  
  25.         if (ChildControlsCreated){
  26.             TextBox part1 = (TextBox)this._container.FindControl("Part1");
  27.             TextBox part2 = (TextBox)this._container.FindControl("Part2");
  28.             TextBox part3 = (TextBox)this._container.FindControl("Part3");
  29.             TextBox part4 = (TextBox)this._container.FindControl("Part4");
  30.             TextBox part5 = (TextBox)this._container.FindControl("Part5");
  31.             part1.Text = this._part1;
  32.             part2.Text = this._part2;
  33.             part3.Text = this._part3;
  34.             part4.Text = this._part4;
  35.             part5.Text = this._part5;
  36.         }
  37.     }
  38. }
Thanks,

Ben.
Dec 3 '08 #1
4 1264
Frinavale
9,735 Expert Mod 8TB
@fosterb
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.Redirect() to redirect the user between the pages then this is probably not the problem....however, if your "license page" is actually a component of the main page then this could be the source of your problem.

-Frinny
Dec 3 '08 #2
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.Redirect 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!!!
Dec 8 '08 #3
Frinavale
9,735 Expert Mod 8TB
@fosterb
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.Redirect. 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...now when you go back to page 2...they are still remembered.

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

-Frinny
Dec 8 '08 #4
Will do.

Thanks again.
Dec 8 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Savvas | last post by:
Hi everybody, I have a lot of textboxes on my form and a "Clear" button. Is there a way with a for loop or something to clear the textboxes, instead of writing textboxName.clear? Thanks a lot
2
by: John Smith | last post by:
Hi folks, I have a form with ASP.NET web controls. At the end of the form there's the "Clear" button to clear the available values and start over. How do I do that? The following did not work...
1
by: Tia Carr | last post by:
When I try to place a child control on top of a panel control, the mouse pointer remains as a pointer instead of changing to a cross. I tried both the ShowGrid on/off, doesn't make a difference.....
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
4
by: Ali | last post by:
I used to clear my page's control in Visual Studio 2003 using code like this: Dim c As Control For Each c In Page.Controls(1).Controls If TypeOf c Is TextBox Then CType(c, TextBox).Text = Nothing...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
by: JohnR | last post by:
Let's say I have an MDI parent form with a textbox. If I create an MDI child form and, at runtime, move the MDI child window over the textbox on the MDI parent, the textbox appears in front of the...
0
by: Qwert | last post by:
Heya, I have a user control (inherits from listview control). I add a textbox control to it as a child control and give the child control the focus. All is well when I type text (keyboard input)...
9
by: Michael.Suarez | last post by:
foreach (TextBox tb in this.Controls) tb.Dispose(); What this does is clears every other textbox on the form. Suppose there are 10 textboxes... this will dispose the 1st one, then i guess...
0
by: Rafe | last post by:
Hi, This seems to be an old question, and I've read back a bit, but rather than assume the answer is "you can't do that", I'd thought I'd post my version of the question along with a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.