Jump to content

Enable View State Problem(ASP.Net)


avisekrishi
 Share

Recommended Posts

I am using ASP.Net Server controls in an .aspx file .The user enter input values and click button "Look Up" which then displays a message .Now the problem is that when i refresh the page, the user input values remain there and also the message.I want that when i refresh the page,the user input values and message doesnt remain there.The page should look as it is opened the very first time. Here is the code:the entire code is written in notepad using extension .aspx.<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestState.WebForm1" enableViewState="False" enableViewStateMac="False"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <script language="C#" runat="server">void SubmitBtn_Click(Object sender,EventArgs e ) { message.Text="Hi "+ Name.Text +",you selected:" + Category.SelectedItem; } </script> </HEAD> <body> <center> <form action="test2.aspx" method="post" runat="server" ID="Form1"> <h3>Name:<asp:textbox id="Name" runat="server" /> Category:<asp:dropdownlist id="Category" runat="server"> <asp:listitem>Psycology</asp:listitem> <asp:listitem>Business</asp:listitem> <asp:listitem>Computer</asp:listitem> </asp:dropdownlist> </h3> <p> <asp:button text="Lookup" OnClick="SubmitBtn_Click" runat="server" ID="Button1" /></p> <p> <asp:label id="message" runat="server" /></p> </form> </center> </body></HTML>

Link to comment
Share on other sites

When you perform a postback on the form (by clicking on the Lookup button), the values remain in the textbox and the message remains until you reload the page from scratch. When you use your browser's refresh button to reload the page it, at least in Firefox, resubmits the page to the server. You might consider adding a "Clear" button to your form.Add this to your script:

<script language="C#" runat="server">void ClearButton_Click(object sender, EventArgs e){	 Response.Redirect(Request.Url.AbsolutePath);}</script>

And this to your form:

<asp:Button id="ClearButton" OnClick="ClearButton_Click" text="Clear Page" runat="server />

Alternatively, you could avoid adding this extra code and then just type in the address of your page in the address bar of your browser and hit ENTER to reload the page rather than clicking the refresh button.

Link to comment
Share on other sites

Hi Jesh , you are right the functionality can be implemented by adding an extra "Clear" button or retyping the URL again .But, i want the functionality to work on refreshing the page only.

When you perform a postback on the form (by clicking on the Lookup button), the values remain in the textbox and the message remains until you reload the page from scratch. When you use your browser's refresh button to reload the page it, at least in Firefox, resubmits the page to the server. You might consider adding a "Clear" button to your form.Add this to your script:
<script language="C#" runat="server">void ClearButton_Click(object sender, EventArgs e){	 Response.Redirect(Request.Url.AbsolutePath);}</script>

And this to your form:

<asp:Button id="ClearButton" OnClick="ClearButton_Click" text="Clear Page" runat="server />

Alternatively, you could avoid adding this extra code and then just type in the address of your page in the address bar of your browser and hit ENTER to reload the page rather than clicking the refresh button.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...