Solved

ListView PostBack ViewState

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

17 Experts available now in Live!

Get 1:1 Help Now