Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Clear TextBox Content + post Back Event. C#

Posted on 2004-08-03
17
2,261 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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