Click here to Skip to main content
15,171,303 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi,

I am reset all textboxes on button click event. but when I refresh page the control values are remain with them.

I have tried EnableViewState=False option but not working.

I saw this article and it is stated that

ViewState and Postback[^]

"An interesting behavior is if we make a control which implements IPostBackEventHandler interface disabled then the ASP.NET will not process the control during postback. So in the above sample, if we make the Textbox (one with EnableViewState = false) disabled then it will not retain the changed value and behave like a label control."

Now, can anyone tell me how can I work with this interface

Thanks,
Bh@gyesh
Posted
Updated 8-May-14 2:11am
v3
Comments
King Fisher 8-May-14 1:58am
   
post your code

This may Help you:
http://www.aspsnippets.com/Articles/Avoid-duplicate-record-insert-on-page-refresh-in-ASPNet.aspx
Bh@gyesh 8-May-14 2:07am
   
Thanks for the link but it will fire postback unnecessary (i.e. twice for one buttonclick)
King Fisher 8-May-14 7:10am
   
what you having?
Bh@gyesh 8-May-14 7:13am
   
<div><br><br>
<table><br><br>
<tr><br><br>
<td><br><br>
Text1 : <asp:TextBox ID="txt1" runat="server" EnableViewState="false" ViewStateMode="Disabled"><br><br>
</td><br><br>
<td><br><br>
Text2 : <asp:TextBox ID="txt2" runat="server" EnableViewState="false"><br><br>
</td><br><br>
<td><br><br>
Text3 : <asp:TextBox ID="txt3" runat="server" EnableViewState="false"><br><br>
</td><br><br>
<td><br><br>
Text4 : <asp:TextBox ID="txt4" runat="server" EnableViewState="false"><br><br>
</td><br><br>
<td><br><br>
<asp:Button ID="btnsubmit" Text="Submit" runat="server" OnClick="btnsubmit_Click" /><br><br>
</td><br><br>
</tr><br><br>
</table><br><br>
</div><br><br>
 <br><br>
<br><br>
protected void Page_Load(object sender, EventArgs e)<br><br>
{<br><br>
<br><br>
}<br><br>
 <br><br>
protected void btnsubmit_Click(object sender, EventArgs e)<br><br>
{<br><br>
 <br><br>
......<br>
txt1.Text = string.Empty;<br><br>
txt2.Text = string.Empty;<br><br>
txt3.Text = string.Empty;<br><br>
txt4.Text = string.Empty;<br><br>
 <br><br>
}

   
v2
Comments
Bh@gyesh 8-May-14 1:49am
   
Thanks for the link but not found solution..!
[no name] 8-May-14 1:55am
   
Did you tried the second solution given in the link..??
Bh@gyesh 8-May-14 1:56am
   
yes but not working :(
[no name] 8-May-14 1:58am
   
Please check the updated link..
Bh@gyesh 8-May-14 2:02am
   
AutoComplete="off" is not working Sorry :(
[no name] 8-May-14 2:04am
   
None of the solution given in that link is working..??
Bh@gyesh 8-May-14 2:07am
   
No..
[no name] 8-May-14 2:09am
   
Please check once and let me know..
Bh@gyesh 8-May-14 2:12am
   
Not working dude... I have checked all solutions..
On page load event try this..

C#
if(IsPostBack)
{
TextBoxControl.Text = String.Empty;
}
   
Comments
Bh@gyesh 8-May-14 7:07am
   
Not working
C#
void CleareAllcontrolsRecursive(Control container)
   {
       foreach (var control in container.Controls)
       {
           if (control is TextBox)
           {
               ((TextBox)control).Text = string.Empty;
           }        
        
       }
   }



call CleareAllcontrolsRecursive(panelcontrol) method in button click event..
and all controls should be in Panel tag...
   
Comments
Bh@gyesh 8-May-14 1:44am
   
Thanks for reply Siva.
The problem is when I refresh page in debug mode, so I can see the textbox value in pageload event.
for eg.
I have buttonsubmit event in which I save the records and clear the textboxes like following

protected void btnsubmit_Click(object sender, EventArgs e)
{
<save content="" code="">
....
...

// Clear controls
txt1.Text = string.Empty;
txt2.Text = string.Empty;
txt3.Text = string.Empty;
txt4.Text = string.Empty;

}

Now, when I press refresh, control comes to this event in which I get old values for all textboxes.
I dont want old values. I want empty values, so validation would be fired.

Let me know if you have any doubt.
Did you have any codes inside page load event?



if(!IsPostBack)
{
//Put that codes here...
}
   
Comments
CHill60 8-May-14 7:35am
   
Would you like to use the "Improve solution" links and update a single solution - for anyone coming across this it is impossible to follow!!
Bh@gyesh 8-May-14 7:47am
   
Hi, Sorry. I am not getting how to use improve solution?
CHill60 8-May-14 8:38am
   
Hi - the comment was directed at Micky Solve as he had posted several solutions. You have correctly been using the "Have a question or Comment?" and the "Reply" links.
For information though, if you hover over the bottom right of a solution you have posted a green "Improve solution" link appears (to the left of the report flag). With enough reputation points you will see this for other peoples posts too. There is a similar mechanism for "Improve question"
Bh@gyesh 8-May-14 8:45am
   
Thanks CHill for information.
Micky Solve 9-May-14 0:06am
   
Sorry, It was my mistake
CHill60 9-May-14 3:02am
   
That's ok ... getting to grips with the site is all - at least you are trying to help the OP :-)
Please try it using jQuery..

$(document).ready(function()
{
$("#TextboxId").val('');
});
   
Comments
Bh@gyesh 8-May-14 6:53am
   
Not working as jquery is called after server call, so at server side at refresh time it retains value. :(
C#
if(!IsPostBack)
{
//not clear textbox
}


else

clear()
   
Comments
Bh@gyesh 8-May-14 8:35am
   
I can not clear textboxes in else part. If doing this it will take empty value though I have entered value in those textboxes.
http://aspadvice.com/blogs/joteke/archive/2004/03/15/2273.aspx[^]


<asp:textbox runat="server" id="Textbox1" autocomplete="off" xmlns:asp="#unknown">





http://stackoverflow.com/questions/1301446/clear-textbox-after-postback[^]


C#
if (IsPostBack)
        {
          textbox1.text = "";
         }


try above methods...
   
Comments
Bh@gyesh 8-May-14 2:41am
   
Already tried Not working...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900