Solved

viewstate disappears on response.redirect

Posted on 2013-01-20
4
800 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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