Note that there are some explanatory texts on larger screens.

plurals
  1. POasp:DataList control with asp:LinkButton inside - something's weird
    primarykey
    data
    text
    <p>I'm working through examples in a book trying to learn ASP.NET, and I've stumbled on something strange in there. First of all, if I type it as it's written in the book, VS gives me errors. This is the code as it's written in the book:</p> <pre><code> &lt;asp:DataList ID="employeesList" runat="server"&gt; &lt;ItemTemplate&gt; &lt;asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" /&gt; Name: &lt;strong&gt;&lt;%#Eval("Name") %&gt;&lt;/strong&gt;&lt;br /&gt; Username: &lt;strong&gt;&lt;%#Eval("Username") %&gt;&lt;/strong&gt;&lt;br /&gt; &lt;asp:LinkButton ID="detailsButton" runat="server" Text=&lt;%#"View more details about " + Eval("Name")%&gt; CommandName="MoreDetailsPlease" CommandArgument=&lt;%#Eval("EmployeeID")%&gt; /&gt; &lt;/ItemTemplate&gt; &lt;SeparatorTemplate&gt; &lt;hr /&gt; &lt;/SeparatorTemplate&gt; &lt;/asp:DataList&gt; </code></pre> <p>So, I've plucked at it for a while, and came up with this solution which actually compiles:</p> <pre><code> &lt;asp:DataList ID="employeesList" runat="server" onitemcommand="employeesList_ItemCommand"&gt; &lt;ItemTemplate&gt; &lt;asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" /&gt; Name: &lt;strong&gt;&lt;%#Eval("Name") %&gt;&lt;/strong&gt;&lt;br /&gt; Username: &lt;strong&gt;&lt;%#Eval("Username") %&gt;&lt;/strong&gt;&lt;br /&gt; &lt;asp:LinkButton ID="detailsButton" runat="server" Text='View more details about &lt;%# Eval("Name") %&gt;' CommandName="MoreDetailsPlease" CommandArgument='&lt;%Eval("EmployeeID") %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;SeparatorTemplate&gt; &lt;hr /&gt; &lt;/SeparatorTemplate&gt; &lt;/asp:DataList&gt; </code></pre> <p>Notice that I've also added the OnItemCommand in the asp:DataList tag, so now I'm able to fire the event as expected.</p> <p>However, results in the browser isn't what I expect; the Name and Username listed in <strong>strong text</strong> show just fine, but the Literal control that should show extra details (the EmployeeID field) and the Name field inside the LinkButton won't show their values in the page:</p> <p><a href="http://lh6.ggpht.com/_x84bQLYH57A/SgxzygartcI/AAAAAAAAAIY/nhT-6RUJa6o/s144/EmployeeDirectory_notshowing.jpg" rel="nofollow noreferrer">not showing as expected http://lh6.ggpht.com/_x84bQLYH57A/SgxzygartcI/AAAAAAAAAIY/nhT-6RUJa6o/s144/EmployeeDirectory_notshowing.jpg</a></p> <p>It <strong>should</strong> say "EmployeeID: 1" and "View more details about Zak Ruvalcaba"</p> <p>I guess it's the Eval function that's not working when inside another control, can anyone help me out?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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