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
Solved

viewstate disappears on response.redirect

Posted on 2013-01-20
4
778 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…

856 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