Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving to session seems to erase value during Repeater's OnItemDataBound Event
    text
    copied!<p>This is weird. I'll try to explain my pain by the following example:</p> <p>I've got an object in the Session: <strong>Session["reportQuestionGroupingTracker"]</strong>. It contains a List of strings. When a string is NOT found, a new h3 header is written in the repeater via a string literal.</p> <p>The problem seems to be the line: <strong>Session["reportQuestionGroupingTracker"] = ary;</strong> This line seems to somehow (black)magically remove the string value in <strong>lit.Text</strong>. The value is there when I breakpoint the code and seems to persist until it goes out of scope in the function(so that part works like expected) - but the string value never seems to make it to the Literal control on the ASP.NET page - they are always blank.</p> <p>Note that if I comment out the problem line or this line: <strong>if (!ary.Contains(headingText))</strong>, the headers show (but too many of them, since every iteration triggers a header write).</p> <pre><code>protected void rptQuestionsGroupedByCountry_OnItemDataBound(object sender, RepeaterItemEventArgs e) { RepeaterItem item = e.Item; ArrayList ary = new ArrayList(); if (null != Session["reportQuestionGroupingTracker"]) ary = (ArrayList)Session["reportQuestionGroupingTracker"]; if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem)) { DataRowView dr = (DataRowView)e.Item.DataItem; string headingText = dr["Heading"].ToString(); Literal lit = (Literal)e.Item.FindControl("LiteralHeader"); if (!ary.Contains(headingText)) { lit.Text = String.Format(@"&lt;h3 class=""questionGroupingHeader""&gt;{0}&lt;/h3&gt;", headingText); lit.Visible = true; ary.Add(headingText); Session["reportQuestionGroupingTracker"] = ary; } } } </code></pre> <p>I've been on this for hours, banging my head - I've done similar things hundreds of times before, I just can't work out why it doesn't work this time! I've tried changing the Repeater to a DataList, Tried using the Context.Items object instead of the Session, a List instead of an ArrayList, but I'm stymied. Help!</p> <p>I've also tried running it in IIS 6, just in case it was some Cassini weirdness, but the output is the same. It's an ASP.NET 4.0 Project.</p> <p>Here's the code from the aspx page:</p> <pre><code> &lt;asp:Repeater ID="rptQuestionsGroupedByCountry" runat="server" OnItemDataBound="rptQuestionsGroupedByCountry_OnItemDataBound"&gt; &lt;HeaderTemplate&gt;&lt;table&gt;&lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt;&lt;td&gt; &lt;asp:Literal ID="LiteralHeader" runat="server" Visible="false" /&gt; &lt;h3 class="report-country-tag"&gt;&lt;%#DataBinder.Eval(Container, "DataItem.Numbers")%&gt;.&lt;%#DataBinder.Eval(Container, "DataItem.QusetionName")%&gt;&lt;/h3&gt; &lt;div class="report-content"&gt; &lt;%#DataBinder.Eval(Container, "DataItem.Answer")%&gt; &lt;p class="date"&gt;Date Updated: &lt;%#DataBinder.Eval(Container, "DataItem.DocumentModifiedWhen")%&gt;&lt;/p&gt; &lt;/div&gt; &lt;/td&gt;&lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt;&lt;/table&gt;&lt;/FooterTemplate&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload