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
Solved

ListView PostBack ViewState

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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