Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing textboxes in a web form

Status
Not open for further replies.

AngieR

Programmer
Feb 2, 2004
5
US
Hello All

How can I clear the contents of let's say 15 textboxes based on a selection from a dropdown list.

Any help will be greatly appreciated !

AngieR
;>)
 
If you still need the ViewState enabled, then you can clear the textboxes in the code behind inside the DropDownList SelectedIndexChanged event handler:
Code:
html:
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
  <asp:DropDownList id=DropDownList1 runat=server AutoPostBack=&quot;True&quot; />
....... textbox controls here....
</form>
code behind:
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
  if(DropDownList1.SelectedValue == someValue)		
  {
    HtmlForm form = (HtmlForm)Page.FindControl(&quot;Form1&quot;);
    foreach(Control ctrl in form.Controls)
    {
      if(ctrl is TextBox)
      {
        ((TextBox)ctrl).Text = &quot;&quot;;
      }
    }
  }
}
 
Thank you for replying to me...forgive me but i could not get it working...is that code C#, i am working on a project using VB...and i am new at programming...
Any suggestions.....Thanks :(
 
I think in VB it should look like this:
Code:
if DropDownList1.SelectedValue = someValue then
  dim form as HtmlForm  = ctype(Page.FindControl(&quot;Form1&quot;), HtmlForm)
  for each Control ctrl in form.Controls
    ' not sure about this line, if VB has the &quot;is&quot; keyword
    if ctrl is TextBox then
      dim txt as TextBox = CType(ctrl, TextBox)
      txt.Text = &quot;&quot;;
    end if
  next
end if
 
If DropDownList1.SelectedValue = someValue Then
Dim form As HtmlForm = CType(Page.FindControl(&quot;Form1&quot;), HtmlForm)
Dim ctrl As Control
For Each ctrl In form.Controls
If ctrl.GetType Is &quot;Whatever type shows up for textbox here&quot; Then
Dim txt As TextBox = CType(ctrl, TextBox)
txt.Text = &quot;&quot;
End If
Next
End If

This is closer, but still not exact i believe
 
I am sorry i took so long to answer. Thank you everyone for your help.

I tried LV's code and it worked...Thanks LV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top