Solved

ListView PostBack ViewState

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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 …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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