Clearing Web Controls Values from a Web Form

I have a web form and after a user submit it I want to clear the form so that they may submit another entry.
I have seen examples in VB of how to loop thru a web form and clear the controls, but I need a C# solution.
I have tried the following code, while it does not give me an error, it does not clear the controls on the form.

Any suggestions? Thanks!

foreach (Control parent in Page.Controls)
        {
            foreach (Control child in parent.Controls)
            {
                if ( child is TextBox )
                {
                    ((TextBox)child).Text = "";
                }
                if ( child is DropDownList )
                {
                    ((DropDownList)child).SelectedItem.Value = "";
                }
             }
         }


--dweezil

SELECT * FROM users WHERE clue = 'yes';

Records found: 0
0
dweezil
8/5/2005 5:50:35 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

10 Replies
1247 Views

Similar Articles

[PageSpeed] 36

Set enableviewstate property to false for each form control.
0
varada
8/5/2005 7:08:59 PM
Just Add an empty string in the dropdownlist boxes.

ListItem lst= new ListItem("");
DropDownList.Items.Add(lst);

Then your code works fine


Thanks
Anitha
0
Anitha
8/5/2005 7:30:26 PM
 Anitha wrote:
Just Add an empty string in the dropdownlist boxes.

ListItem lst= new ListItem("");
DropDownList.Items.Add(lst);

Then your code works fine


Thanks
Anitha

Each DropDownList already has a default selection value of "".

I'll have to try to enableviewstate=false setting when I get back to the office.

Thanks guys!

--dweezil

SELECT * FROM users WHERE clue = 'yes';

Records found: 0
0
dweezil
8/5/2005 11:46:04 PM
 varada wrote:
Set enableviewstate property to false for each form control.

This did not work. The controls on the form are not being cleared.

In fact, I had to remove enableviewstate=false from the DropDownList controls.
It was giving an error when I tried to insert the DropDownList selection into the database -- because it was null.



--dweezil

SELECT * FROM users WHERE clue = 'yes';

Records found: 0
0
dweezil
8/8/2005 1:59:12 PM
Do you want to clear the values or control itslef from the form?
0
varada
8/8/2005 3:03:43 PM
 varada wrote:
Do you want to clear the values or control itslef from the form?

Only the values!

Basically, after a user submit a form to enter a record into the database.
I want to clear the form so the user can add another record.

--dweezil

SELECT * FROM users WHERE clue = 'yes';

Records found: 0
0
dweezil
8/8/2005 4:05:03 PM
After you enter the record into database, clear the formfields by setting to controlname.text property to empty value ("")

if you have a textbox say txtUsername then clear the value of the text with txtUsername.text=""
if you have a dropdown say ddCountryList then set the index of dropdown to 0 by using ddCountryList.selectedindex=0


0
varada
8/8/2005 6:30:04 PM
 varada wrote:
After you enter the record into database, clear the formfields by setting to controlname.text property to empty value ("")

if you have a textbox say txtUsername then clear the value of the text with txtUsername.text=""
if you have a dropdown say ddCountryList then set the index of dropdown to 0 by using ddCountryList.selectedindex=0



I know how to reset a controls's value manually.
I want to be able to loop through all the controls on a page programatically and reset their values. This way I can reuse the code on any webpage.
The code on my original post -- I thought would do this, but it doesn't.

--dweezil

SELECT * FROM users WHERE clue = 'yes';

Records found: 0
0
dweezil
8/8/2005 7:08:09 PM

Try this,

private void btnSubmit_Click(object sender, System.EventArgs e)

{

         foreach(Control parent in Page.Controls)

         {

            foreach(Control Child in parent.Controls)   

               {

               if(Child is TextBox)

               ((TextBox)Child).Text="";

               if(Child is DropDownList)

               ((DropDownList)Child).SelectedIndex=0;

               }

      }

}



<HTML>
<Head>

<title>ClearValues</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  </Head>
  
<body MS_POSITIONING="GridLayout" id="bodytag">
  <form id="Form1" method="post" runat="server">
   <asp:TextBox id="txtName" style="Z-INDEX: 101; LEFT: 192px; POSITION: absolute; TOP: 56px" runat="server"></asp:TextBox>
   <asp:TextBox id="txtAge" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 96px" runat="server"></asp:TextBox>
   <asp:DropDownList id="dljob" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 144px" runat="server"
    Width="136px">
    <asp:ListItem  Selected="True"></asp:ListItem>
    <asp:ListItem Value="1">IT Department</asp:ListItem>
    <asp:ListItem Value="2">Mechanical Department</asp:ListItem>
    <asp:ListItem Value="3">Electronics Engineer</asp:ListItem>
    <asp:ListItem Value="4">Electrical  engineer></asp:ListItem>
    </asp:DropDownList>
   <asp:DropDownList id="dldept" style="Z-INDEX: 104; LEFT: 216px; POSITION: absolute; TOP: 176px" runat="server"
    Width="144px">
    <asp:ListItem Selected="True"></asp:ListItem>
    <asp:ListItem Value="1">Civil Engineer</asp:ListItem>
    <asp:ListItem Value="2">Software Engineer</asp:ListItem>
    <asp:ListItem Value="3">Mechanical Engineer</asp:ListItem>
    <asp:ListItem Value="4">Electronics engineer></asp:ListItem>
   </asp:DropDownList>
   <asp:Button id="btnSubmit" style="Z-INDEX: 105; LEFT: 232px; POSITION: absolute; TOP: 256px"
    runat="server" Text="Submit"></asp:Button>
  </form>
 </body>
</HTML>

0
Anitha
8/9/2005 3:27:29 PM
 Anitha wrote:

Try this,

private void btnSubmit_Click(object sender, System.EventArgs e)

{

         foreach(Control parent in Page.Controls)

         {

            foreach(Control Child in parent.Controls)   

               {

               if(Child is TextBox)

               ((TextBox)Child).Text="";

               if(Child is DropDownList)

               ((DropDownList)Child).SelectedIndex=0;

               }

      }

}



<HTML>
<Head>

<title>ClearValues</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  </Head>
  
<body MS_POSITIONING="GridLayout" id="bodytag">
  <form id="Form1" method="post" runat="server">
   <asp:TextBox id="txtName" style="Z-INDEX: 101; LEFT: 192px; POSITION: absolute; TOP: 56px" runat="server"></asp:TextBox>
   <asp:TextBox id="txtAge" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 96px" runat="server"></asp:TextBox>
   <asp:DropDownList id="dljob" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 144px" runat="server"
    Width="136px">
    <asp:ListItem  Selected="True"></asp:ListItem>
    <asp:ListItem Value="1">IT Department</asp:ListItem>
    <asp:ListItem Value="2">Mechanical Department</asp:ListItem>
    <asp:ListItem Value="3">Electronics Engineer</asp:ListItem>
    <asp:ListItem Value="4">Electrical  engineer></asp:ListItem>
    </asp:DropDownList>
   <asp:DropDownList id="dldept" style="Z-INDEX: 104; LEFT: 216px; POSITION: absolute; TOP: 176px" runat="server"
    Width="144px">
    <asp:ListItem Selected="True"></asp:ListItem>
    <asp:ListItem Value="1">Civil Engineer</asp:ListItem>
    <asp:ListItem Value="2">Software Engineer</asp:ListItem>
    <asp:ListItem Value="3">Mechanical Engineer</asp:ListItem>
    <asp:ListItem Value="4">Electronics engineer></asp:ListItem>
   </asp:DropDownList>
   <asp:Button id="btnSubmit" style="Z-INDEX: 105; LEFT: 232px; POSITION: absolute; TOP: 256px"
    runat="server" Text="Submit"></asp:Button>
  </form>
 </body>
</HTML>


This is the exact same code that I posted in my original post!

This code is not clearing the values from my web controls on the form. Does anybody know why?

--dweezil

SELECT * FROM users WHERE clue = 'yes';

Records found: 0
0
dweezil
8/18/2005 1:06:48 PM
Reply:

Similar Artilces:

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

Passing Values from a Web Form to Another Web Form
Hello All! I am trying to pass values from one web form to another web form. I know that there are 3 ways of doing this: queryString, Server.Transfer and Session. I am using the Server.Transfer method. But I am not able to create, in my 2nd web form, an instance of my 1st web form. My 1st Web form is named CallTrack.aspx and my 2nd is named Export.aspx. What I mean is something like this: Public value as CallTrack. I can't create it in my 2nd web form because when the intellisense is displayed, there's no CallTrack in the options. Thanks!   How about using cross-...

web web web
Name: bahadir Email: sensiz_olmuyor_t1_at_hotmail.com Product: Firefox 2 Beta 2 Summary: web web web Comments: web sayfası yapmak Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.8.1b2) Gecko/20060821 Firefox/2.0b2 ...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

Retreiving a value from a web form within a web user control
Hi  I have a page (web form) that retreives some data from a database. To be more specific: at some point when user clicks a button (in the web form) there is some ID number retreived from a database in code behind. At the same time the Visible property of the nestet web user control is chamged to true. I want than forward the ID to the web user control so that a grid view can load data from a database for that ID. How can I access that data (the ID) within a web user control context nested in that page?  Kind regards   Check this ;ink  http://www.codeproject.com/K...

For interrupting.web form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form
Web Form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form ! Because of my pool English, if you can't see , I will explain more ! ThanksGambler is the real life I guess you mean when form grows it also grow parent div element. This is normal. If you want div to stay fixed size and rather clip the form and maybe show a scrollbar, look at overflow property in CSS.Thanks------------------------------------------------------------This posting is provided "AS IS" with no warranties, and confers no ...

calling a web form from another web form
I would like to know what is the best way to open a web form from another web form. I would like to call it from a button control. Thanks Well, if I've grasped your question right, there are many methods to do that: 1)Response.Redirect > To navigate to another page from code and is equal to clicking a hyperlink. I suggest this as I think it's closer to your question. 2)Server.Transfer > This ends the current web form and begins a new web form. It's usable when you want to pass some variables i.e (Label1.Text) to the next immediate page. It's not safe though and not used to send p...

How to fire events in other web form from other web form ?
Suppose that I have A and B web form. I want to trigger B web form's Page_Load event from A web form. How can I do that ? Thanks in advanced WilbertWilbert Bujn Create a public method in B that call the load method : public void Load(object sender, System.EventArgs e) { Page_Load(sender, e) ; } In the page_load of A, instanciate a B object and then call the function B myB = new B() ; myB.Load() ; Quicky Thank you quicky, it works fine. WilbertWilbert Bujn...

Web Form embedded in another Web Form
Hi,  I have programmed a Web Form which acts like a scientific calculator. I would like to embedd this calculator in the upper left hand corner of another accounting Web Form. I don't want to host the calculator on the same server as the accounting page. I.e. I want them to have distinct URLs. Classically, I would do this using frames, but is there a better way using ASP? Is there some sort of Panel which can display the webpage at a specific URL which I can just drag onto my accounting Web Form? Thanks,  P1000   Frames are the only way to embed a webform inside ano...

Include web form inside web form
I tried to include an aspx file in a asp.net web form. However, all the web controls (textbox, button) in the included page are not displayed when called. <Form id="Form1" method="post" runat="server"> <%   Response.writeFile("subPage.aspx"); %> <asp:button id="btnPrev" runat="server" text="Previous"></asp:button> </Form> There are some <asp:textbox> controls in the subPage.aspx file, the file itself works fine, but when it is included in the main page, all the controls are not displayed. Please assist. Thanks   u should u...

Web Form vs. Web Content Form
If you don't mind, I would like to know what is the difference between those two items, when I go to add a new item I see both of those and I don't know what the difference is.  A content form is for use with a masterpage. A web form is a standalone web page. If you select web content form, you will then have to specify a masterpage for it. and what is a master page used for?  Hi, a master page is used to give a constant look and theme to a site, without having to duplicate code. So basically you can put your header, menu, nav and footer on a master page. Then a...

Web Form and Web User Control
Hello, I have a Web User Control (ascx) that includes a button. This button has an onclick event method. This Web User control is included into a Web Form. (thru LoadControl). However, when I click on the button, the event method is not executed. What should I include in order it works fine? Thanks in advance. GAA. Using Loadcontrol is using dynamic controls and when using dynamic controls you must recreate the same dynamic controls ono postback to econstruct the page control tree. It is best if you do this during the Page PreInit event. How complex are your controls ...

Web resources about - Clearing Web Controls Values from a Web Form - asp.net.web-forms

Market clearing - Wikipedia, the free encyclopedia
When markets clear, they are priced so that the entire supply is sold. However, retail stores usually restock goods as they are sold. In simple ...

Social Media Jobs: Publishers Clearing House, Travelzoo, Southern Poverty Law Center
This week, Publishers Clearing House is hiring a social media marketing manager , as well as a social media coordinator . Meanwhile, Travelzoo ...

Chakra Clearing - Doreen Virtue, Ph.D. on the App Store on iTunes
Get Chakra Clearing - Doreen Virtue, Ph.D. on the App Store. See screenshots and ratings, and read customer reviews.

Clearing - Flickr - Photo Sharing!
Explore Several seconds' photos on Flickr. Several seconds has uploaded 2807 photos to Flickr.

Snowplows Clearing Highway 401 in Toronto - YouTube
View from my condo window at Yonge/401 watching the snowplows clear the highway. Wish I didn't have the radio on or you would hear the loud sound, ...

Houston eyes future benefits after clearing out veterans
Houston eyes future benefits after clearing out veterans

Labor says trade union royal commission submissions clearing Bill Shorten released at ‘odd’ time - AdelaideNow ...
OPPOSITION Leader Bill Shorten says he is pleased to be cleared of any wrongdoing during his time as boss of the Victorian branch of the Australian ...

Essendon drugs saga: Clearing players and clearing club not the same thing
The Essendon players' drugs tribunal has become more a referendum on whether the club was guilty rather than a test of the players' guilt.

Clearing the air on a fibre-to-the-node NBN
With FTTN and HFC now set to form the backbone of the NBN, it’s time for NBN Co to shed some further light on the design rules.

Angelina Jolie quits Princess Diana's favourite mine-clearing charity after row over payments
Britain's charity watchdog is reviewing a complaint about trustees' pay at a global mine-clearing charity made famous by Princess Diana following ...

Resources last updated: 1/13/2016 8:05:43 PM