Solved

viewstate disappears on response.redirect

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fix 'event logs could not be searched' error asp.net? 3 26
Need to find a web event on the IIS server 4 54
Put File 6 37
Javascript function 3 22
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

752 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