Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ListView PostBack ViewState

Posted on 2013-01-31
2
Medium Priority
?
819 Views
Last Modified: 2013-01-31
Hi,

I have been working through an issue for the last several days.

I have a ListView that has the following ItemTemplate and corresponding EditItemTemplate:

            <ItemTemplate>
                <asp:PlaceHolder runat="server" ID="categoryHeadingPlaceHolder" >
                    <tr style="background-color:Gray;">
                        <td colspan="3" ><font color="white"><%# Eval("Category") %></font></td>
                    </tr>
                </asp:PlaceHolder>
                <tr style="<asp:Literal runat="server" ID="rowStyleLiteral" />">
                    <td>
                        <asp:PlaceHolder runat="server" ID="controlsPlaceHolder" >
                            <asp:ImageButton runat="server" ID="editControl" CommandName="Edit" ImageUrl="_images/icons/edit.gif"  />
                            <asp:ImageButton runat="server" ID="deleteControl" CommandName="Delete" ImageUrl="_images/icons/delete.gif"  />
                        </asp:PlaceHolder>
                        <a href="Document.aspx?GUID=<%# Eval("GUID") %>" ><%# Eval("displayName") %></a>
                    </td>
                    <td>
                        <%# (null == Eval("postedDate") ) ? string.Empty : Eval("postedDate", "{0:M/d/yyyy}") %>
                    </td>
                    <td>
                        <%# (null == Eval("effectiveDate")) ? string.Empty : Eval("effectiveDate", "{0:M/d/yyyy}")%>
                    </td>
                </tr>
            </ItemTemplate>

            <EditItemTemplate>
                <asp:PlaceHolder runat="server" ID="categoryHeadingPlaceHolder" >
                    <tr style="background-color:Gray;">
                        <td colspan="3" ><font color="white"><%# Eval("Category") %></font></td>
                    </tr>
                </asp:PlaceHolder>
                <tr style="<asp:Literal runat="server" ID="rowStyleLiteral" />">
                    <td>
                        <asp:PlaceHolder runat="server" ID="controlsPlaceHolder" >
                            <asp:ImageButton runat="server" ID="updateControl" CommandName="Update" ImageUrl="_images/icons/save.gif"  />
                            <asp:ImageButton runat="server" ID="cancelControl" CommandName="Cancel" ImageUrl="_images/icons/delete.gif"  />
                        </asp:PlaceHolder>
                        <asp:TextBox runat="server" ID="displayNameTextBox" Text='<%# Eval("displayName") %>' Columns="50" />
                        <asp:RequiredFieldValidator runat="server" ID="displayNameRqdVal" ControlToValidate="displayNameTextBox" Text="(required)" />
                        <br /><asp:DropDownList runat="server" ID="categoryDropDownList" />
                    </td>
                    <td>
                        <%# (null == Eval("postedDate") ) ? string.Empty : Eval("postedDate", "{0:M/d/yyyy}") %>
                    </td>
                    <td>
                        <asp:Calendar
                            runat="server"
                            ID="effectiveDateCalender"
                            SelectedDate='<%# ( null == Eval("effectiveDate") ) ? DateTime.Today : Eval("effectiveDate") %>' />
                    </td>
                </tr>
            </EditItemTemplate>

Open in new window


The issue is with the PlaceHolder control within the templates.  I have the following EventHandler:

        protected void documentListView_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            ((PlaceHolder)e.Item.FindControl("categoryHeadingPlaceHolder")).Visible = false;
            string currentCategory = DataBinder.Eval(e.Item.DataItem, "Category").ToString();
            if (!tmpCategoryName.Equals(currentCategory))
            {
                tmpCategoryName = currentCategory;
                ((PlaceHolder)e.Item.FindControl("categoryHeadingPlaceHolder")).Visible = true;
                tempBackgroundFlag = true;
            }

            ((PlaceHolder)e.Item.FindControl("controlsPlaceHolder")).Visible = false;
            if ((int)Session[Constants.Applications.AuditPortalString] > Constants.Permission.Readonly)
            {
                ((PlaceHolder)e.Item.FindControl("controlsPlaceHolder")).Visible = true;
            }


            if (tempBackgroundFlag)
            {
                ((Literal)e.Item.FindControl("rowStyleLiteral")).Text = "background-color:white;";
            }
            else
            {
                ((Literal)e.Item.FindControl("rowStyleLiteral")).Text = "background-color:lightyellow;";
            }

            tempBackgroundFlag = !tempBackgroundFlag;

        }

Open in new window


This event handler does 2 things, 1 it checks to see if the category of the current item is the same as the previous.  If it is, it hides the control.  The second task is to set the background color of the row.  

Everything works fine and the output is great except for the following sequence of events.  
1) a row is selected for editing
2) the date of the calendar control is changed.

The forces a postback.  Which shows all the category heading fields and removes all the background colors from the rows.

Doing a debug, confirms that the ItemCreated event is not firing on the post back, so the formatting is not being applied.

Theoritcally these are dynamically created controls which is within the Page_Load, thus are not loaded into the viewState.  

I tried moving the databinds into Page_Init.  The initial view worked fine, but when I went to edit any of the rows, the first row that fired ItemCreated had null references for the dataItem.  I moved it back and haven't explored it further.

Testing the dynamically created controls theory, there are several controls that are being placed in the item.  The control which initiates the problem is the calendar control.  I tested the theory by changing the text in the TextBox control, which doesn't force a postBack and then changing the calendar date which does force a postback.  The text stays.  What this tells me is that the controls added to the page via the ItemTemplate and EditItemTemplate are being added to ViewState, which should then include the PlaceHolder and tr controls which I am having difficulty with.

Thanks for the reading to the end and I look forward to a healthy discussion on the topic.
0
Comment
Question by:infinidem
2 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 1500 total points
ID: 38840209
Is it possible to move the code to the ItemDataBound event instead of the ItemCreated event?
0
 

Author Closing Comment

by:infinidem
ID: 38840538
That worked.

Now the question is why.  Just like the ItemCreated event it is not fired during the postback from the Calendar control.  

The answer must be with the Page Lifecycle.  Yet ItemDataBound is raised after ItemCreated.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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