Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

viewstate disappears on response.redirect

Posted on 2013-01-20
4
Medium Priority
?
831 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 1000 total points
ID: 38799376
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38799846
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 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