Clear TextBox Content + post Back Event. C#

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.
LVL 1
skarra1Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
gregoryyoungConnect With a Mentor Commented:
you are creating textboxes every time you postback and associating them with the same id ... this could be causing the problem.
0
 
gregoryyoungCommented:
turn off viewstate for it ... EnableViewState = false ... either that or explicitly set the .Text = "" when you show it.
0
 
skarra1Author Commented:
i did do both but it does not help..
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
skarra1Author Commented:
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
 
gregoryyoungCommented:
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
 
skarra1Author Commented:
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
 
gregoryyoungCommented:
would it be possible for to put a quick test script showing the problem ?
0
 
skarra1Author Commented:
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
 
skarra1Author Commented:
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
 
gregoryyoungCommented:
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
 
skarra1Author Commented:
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
 
gregoryyoungCommented:
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
 
skarra1Author Commented:
Am I not setting the ID right in my above Code? I think I am .  Pls let me know.
0
 
TheAvengerConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.