I created a form with two text boxes, when i reload the page those values are not getting refreshed..
Kindly help to clear the values of textboxes when the page is loaded.

Option which i used

Auto Post Back=False
Enable View State=False

Dani AI

Generated

Brief explanation first: ASP.NET page lifecycle is the root cause. Control values from the browser are restored before Page_Load, and Page_Load runs before any button event handlers. That means clearing fields inside Page_Load on a postback will wipe the inputs before the click handler reads them; conversely, checking Page.IsPostBack inside the button click is redundant (as and noted). Also note that setting EnableViewState = false does not prevent posted form values from reaching the server — Request.Form still repopulates textbox values on postback.

Practical, reliable approaches:

  • Server-side: read and validate the inputs in the button click, compute the result, then clear the textbox values immediately after processing. To avoid browser re-submitting or re-populating the form on refresh, apply the Post-Redirect-Get pattern: store the result temporarily (Session or QueryString) and call a redirect to the same page so the browser ends up on a clean GET. Example pattern (simplified):
    
    /* in Button click: validate, save result, then */
    Session["sum"] = result;
    Response.Redirect(Request.RawUrl);

/ in Page_Load: if Session["sum"] exists, show it and remove it /


This prevents F5 from re-posting and keeps inputs cleared.

- Client-side: a small script can detect a reload and clear inputs before the browser shows them (useful if browser autofill or reload is the issue). Modern approach uses the Navigation API with a fallback to performance.navigation.

Dependent dropdowns (follow-up): server-side is simplest without data binding — set the first dropdown to AutoPostBack and in its SelectedIndexChanged handler call `ddl2.Items.Clear()` and add new ListItem entries based on the selected value. Client-side is also fine (as @YanivC suggested): handle the change event in JS and repopulate the second select from a small lookup object for a snappier UI.

Cautions: always validate/parsetext (use TryParse) before arithmetic and avoid relying on client-side clearing for security or correctness.

Recommended Answers

All 10 Replies

>Kindly help to clear the values of textboxes when the page is loaded.

TextBox1.Text=String.Empty;  
...

Hey Thanks for your reply, my actual problem is - i am not able to clear the values of textboxes because i am adding the two values in the click event (i.e) after the pageload

I have entered my source code below:

=========================================

namespace Sample2
{
    public partial class _Default : System.Web.UI.Page
    {
        public void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int value = Int32.Parse(TextBox1.Text) + Int32.Parse(TextBox2.Text);

            Label3.Visible = true;
            // Response.Write ("the result is" +value);

            Label3.Text = "The result is" + value;
           // Response.Redirect("//www.google.com");
            
            

            if (Page.IsPostBack == true)
            {
                
                TextBox1.Text = "";
                TextBox2.Text = "";
               

                //  Label3.Visible = false;

                // Label3.Text = "The result is" + value;
                //  Response.Write("HI");

            }
          

        }

=================================================

You code seems alright.

I know this is a 4 day old thread but I have never seen a check for (Page.IsPostBack) in a button_click event. I thought that you could only check for a postback in the Page_Load Event.

Am I wrong in thinking this?

>Am I wrong in thinking this?

You are right. The statement IsPostback in button's click event has no value at all.

ahhhmmm.... i don't know really know.. if it can help you..hehehe
but just try this one..
select in the event the activate and the object is form1..if you don't rename the form...between the private sub form1.activate
end sub...type the text1.text="" and text2.text=""
so the code will be...May God Bless you... i suggest that it is a good habit to rename the controls..

Welcome ragde51.

Please read OP and other subsequent posts. This is ASP.NET forum.

Also, you could mix some javascript in this.
<script lanague="javascript">
document.getElementById("<%=TextBox1.ClientID%>").value = "";
</script>

and call this in the onload.

Hi
I have a dropdownlist (ddl2) which is dependent on the selected value of another dropdownlist (ddl1).
I am not using databinding methods. Kindly prove the syntax for this dropdown list!!

again,i would use javascript for some thing like this
the first list you can use <select change="someFunction()">
then that function would act on the 2nd list

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.