Solved

viewstate disappears on response.redirect

Posted on 2013-01-20
4
758 Views
Last Modified: 2013-01-23
on my asp.net c# page I have two text boxes(start  and end dates) with CalendarExtenders. User selects start date and the end date. on end date selection I bind my grid
 protected void calEndDate_TextChanged(object sender, EventArgs e)
    {
        BindGrid();
    }

Open in new window


in the grid i have a command button with the following code
 protected void gvAllRoomStatus_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Manage")
        {

            GridViewRow row = gvAllRoomStatus.Rows[Convert.ToInt16(e.CommandArgument)];

            int BookingID = Convert.ToInt32(row.Cells[1].Text);
           

            DataClassesDataContext context = new DataClassesDataContext();

           
                Session["BookingID"] = BookingID;

            
                Response.Redirect("CheckIn.aspx");

            
        }
    }

Open in new window


when the user go that page and clicks the back button, all the selected dates and the gridview data disappears. Any ideas why the viewstate is disappearing

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div style="margin: 0.5em;">
            <div id="left" style="width: 800px; height: 100%;">
                <table cellspacing="2" style="padding-left: 5px;">
                    <tr>
                        <td colspan="2" valign="top" style="font-weight: 700; text-align: left">
                            <h2>
                                Bookings status</h2>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                            <asp:Label ID="Label1" runat="server" Text="From Date:">  
                            </asp:Label>
                        </td>
                        <td valign="top">
                            <div>
                                <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="calStartDate"
                                    CssClass="cal_Theme1">
                                </cc1:CalendarExtender>
                                <asp:TextBox ID="calStartDate" runat="server" Width="175px" Style="padding-left: 6px;"></asp:TextBox>
                            </div>
                            <br />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                            <asp:Label ID="Label2" runat="server" Text="To Date:">  
                            </asp:Label>
                        </td>
                        <td valign="top">
                            <div>
                                <cc1:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="calEndDate"
                                    CssClass="cal_Theme1">
                                </cc1:CalendarExtender>
                                <asp:TextBox ID="calEndDate" runat="server" Width="175px" Style="padding-left: 6px;"
                                    AutoPostBack="True" OnTextChanged="calEndDate_TextChanged"></asp:TextBox>
                            </div>
                            <br />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" valign="top">
                            <asp:GridView ID="gvAllRoomStatus" runat="server" CellPadding="5" Style="margin-left: 5px;
                                margin-top: -1em;" ForeColor="Black" Width="750px" Height="100%" GridLines="Horizontal"
                                BackColor="#F6E9C4" BorderColor="Tan" BorderWidth="1px" OnRowCommand="gvAllRoomStatus_RowCommand">
                                <AlternatingRowStyle BackColor="#F6E9C4" />
                                <FooterStyle BackColor="Tan" />
                                <HeaderStyle BackColor="#DCAE25" Font-Bold="True" HorizontalAlign="Left" />
                                <PagerStyle BackColor="#F6E9C4" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
                                <Columns>
                                    <asp:ButtonField ButtonType="Button" HeaderText="" HeaderStyle-HorizontalAlign="Left"
                                        HeaderStyle-Width="75" CommandName="Manage" Text="Select" />
                                </Columns>
                            </asp:GridView>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="footer">
            </div>
    </ContentTemplate>
</asp:UpdatePanel>

Open in new window

0
Comment
Question by:mmalik15
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Andr3wC
ID: 38799366
Hi I believe you will have too wrap your binding code with a if ispostback check this will check if the page is a postback or not and set control bindings accordingly.
0
 
LVL 6

Accepted Solution

by:
Andr3wC earned 250 total points
ID: 38799376
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38799846
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 250 total points
ID: 38809554
After a redirect you will loose any state information associated to your controls. If you simply want the page to refresh, remove the redirect. After the code has finished executing, the page will refresh and any state will be kept.

Behind the scenes, this works because ASP.NET writes the state information to a hidden input field on the page. When you click a button, the form is posted and ASP.NET deciphers the viewstate. Your code runs, modifying the state, and after that the state is again written to the hidden field and the cycle continues, until you change the page without a POST. This can happen when clicking an hyperlink to another page, or via Response.Redirect(), which instructs the browser to follow the specified url.

If Response.redirect is used, then ViewState cannot be accessed across pages.

Access viewstate across pages
http://www.dotnetlogix.com/Article/aspnet/67/Pass-Viewstate-across-pages-in-ASP.NET.html
http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now