Solved

Clear TextBox Content + post Back Event. C#

Posted on 2004-08-03
17
2,248 Views
Last Modified: 2007-12-19
I am having a wearied problem. I am working on C# for web app. I have designed a form which has a drop down. Based on the selection in the drop down I am dynamically creating either textbox’s or drop down’s. Ex: When I say SSN: 1 textbox. When I say ADDRESS: 2 textbox and 1 drop down., etc.

[Implemented this article concept to design the dynamic textboxs: http://weblogs.asp.net/ksamaschke/archive/2003/04/27/6098.aspx]

What happens is:
Initially let me select SSN and I will have a textbox and I run my query by hitting submit form button. After that I will go back and then change from selection from SSN to ADDRESS and then I will get the 2 textbox and 1 drop down.

But I have a problem here. My textbox tries to remember the previous punched in SSN instead of clearing it. I tried various ways of clearing the textbox but does not happen?

Any ideas? I know it is something to do with postback event I guess. Pls suggest.
0
Comment
Question by:skarra1
  • 7
  • 6
17 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11708897
turn off viewstate for it ... EnableViewState = false ... either that or explicitly set the .Text = "" when you show it.
0
 
LVL 1

Author Comment

by:skarra1
ID: 11709118
i did do both but it does not help..
0
 
LVL 1

Author Comment

by:skarra1
ID: 11709169
Just for information. When i try to debug the application it does not show me that i have a textbox at that location.
I tried to use this method to clear the contents but guess what is does not find the textbox when it goes into this loop.

private void LoopTextboxes()
{
      foreach (Control c in this.Controls)
      {
            if (c is TextBox)
            {
                  TextBox t = (TextBox) c;
                  t.Text = "";
            }
      }
}

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11709210
umm that function needs to be recursive ... esp if you add to a placeholder etc ...

private void LoopTextboxes(ControlCollection cc)
{
     foreach (Control c in cc)
     {
          if (c is TextBox)
          {
               TextBox t = (TextBox) c;
               t.Text = "";
          }
          if(c.Controls.Count > 0) {
               LoopTextBoxes(c.Controls);
          }
     }
}

LoopTextBoxes(this.Controls)
0
 
LVL 1

Author Comment

by:skarra1
ID: 11709466
yup. I am adding it to the place holder but the code you have provided does not help as when it goes through this loop it does not find a textbox control in the page so it does not execute the if statement. Hence it is not getting cleared. I am not sure why this happens.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11709485
would it be possible for to put a quick test script showing the problem ?
0
 
LVL 1

Author Comment

by:skarra1
ID: 11709491
I mean to say. I can see the textbox in the screen with the old values but when i debug the application it does not find or locate the textbox. Sounds crazy right? Do you want me to post the code.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:skarra1
ID: 11709550
Page_ load:
{
..........
if (Page.IsPostBack)
{
string dropDown = cbxCriteria.SelectedItem.Text;
if(dropDown == "SSN")
{
        CreateSSNTextBox();
}
if(dropDown == "Address")
{
        CreateAddressTextBox();
}
......
}

private void btnSubmit_Click(object sender, System.EventArgs e)
{
string dropDown = cbxCriteria.SelectedItem.ToString();
if(dropDown == "SSN")
{
txtValue = new TextBox();
txtValue = (TextBox)this.Page.FindControl("txtValue");
string Value = txtValue.Text;
                  
DS = sots.searchSpecificColumn("SearchBySSN","SSN",Value);
}
......
}

public void CreateSSNTextBox()
{
            string tooltip = "Enter a Valid SSN (9 digits, No spaces or dashes)";
            int maxlen = 9;
            InvisibleAll(false);
            pnlTable3.Visible = true;
            lblValue.Text = "SS Number";
            lblValue.Visible = true;
                                LoopTextboxes(this.Controls);
            txtValue = CreateTextBox("txtValue", maxlen, tooltip); // creates the textbox. Works fine.
                      PlaceHolder10.Controls.Add(txtValue);
            txtValue.Text = "";
}

private void cbxCriteria_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  string dropDown = cbxCriteria.SelectedItem.ToString();
                  if(dropDown == "No Criteria")
                  {
                        InvisibleAll(false);
                        pnlTable3.Visible = false;
                  }
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11709594
why are you creating them every time it is switched ?

also
txtValue = new TextBox(); (don't need this)
txtValue = (TextBox)this.Page.FindControl("txtValue");
string Value = txtValue.Text;

in CreateTextBox are you properly setting the ID field of the textbox you are creating ?
0
 
LVL 1

Author Comment

by:skarra1
ID: 11709645
If i don;t it gives me NULL VALUE EXCEPTION at txtValue. Then i read that article and then implemented it accordingly.
http://weblogs.asp.net/ksamaschke/archive/2003/04/27/6098.aspx

txtValue = CreateTextBox("txtValue", maxlen, tooltip); // creates the textbox. Works fine.

public TextBox CreateTextBox(string name, int maxlen, string tooltip)
            {
                  try
                  {
                        TextBox txtBox = new TextBox();
                        txtBox.ID = name;
                        txtBox.Text = "";
                        txtBox.Visible = true;
                        txtBox.MaxLength = maxlen;
                        txtBox.BackColor = Color.Yellow;
                        txtBox.ForeColor = Color.FromName("#400000");
                        txtBox.Font.Bold = true;
                        txtBox.Font.Name = "Verdana";
                        txtBox.Font.Size = FontUnit.XSmall;
                        txtBox.ToolTip = tooltip;
                        return txtBox;
                  }
                  catch(Exception ex)
                  {
                        throw new Exception("Exception in CreateTextBox: " + ex.ToString());
                  }
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11709692
jsut making sure your ID is set ....

as for the null value it would be because its not finding your control but it should still be null you are setting the reference and then throwing it away.
0
 
LVL 1

Author Comment

by:skarra1
ID: 11709707
Am I not setting the ID right in my above Code? I think I am .  Pls let me know.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 50 total points
ID: 11709873
you are creating textboxes every time you postback and associating them with the same id ... this could be causing the problem.
0
 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 50 total points
ID: 11713286
I suppose the problem is that you text box is created dynamically. It works like this:

- First page load: in Page_Load you create the text box
- First submit: the page is initialized, then the view state is read, then page_load is called again. Note: up to here your text box was not recreated and does not exist. You need to recreate it here! This is very important
- Next step is that APS.NET will make another view state check and set your text box properties. Here! After Page_Load has finished!
- Next the button click or whatever post action will be called. Here you can work with the text box, clear its content, etc.
- The new page is created and sent to the user

It is very important to follow the steps. It's a tricky business actually. You can read something about the lifecycle here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp

And something about dynamic controls:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingcontrolstowebformspageprogrammatically.asp
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mutually exclusive checkbox in a gridview 18 41
XML to SQL Table using c# 5 46
Expando 4 34
Trouble with References... 5 22
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now