Solved

ListView PostBack ViewState

Posted on 2013-01-31
2
768 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now