0

I'm new to .NET and web development in general, and have been working on code someone else wrote but can't figure out what this particular syntax is doing. I have a telerik RadGrid that is displaying data dynamically from a tsql database, and am using a GridTemplateColumn ItemTemplate to display a asp:Label control. The Text value of that control is Text='<%# Eval("RoleCode") %>'. "RoleCode" corresponds to a row returned by a stored procedure, "Eval" is a class that I'm not using on the page, but have used elsewhere in my application to (I think) access the properties stored in that class. (RoleCode is a property in that class.) My code works, but since the Eval class is not being used on this page, I don't understand why it works.

Is this the same kind of code as the <%@ Page ... %> code at the top of my page?

<telerik:RadGrid ID="grd1" runat="server" AllowSorting="true" OnNeedDataSource="grd1_NeedDataSource"
    <MasterTableView ShowHeader="true" ShowFooter="false" CommandItemDisplay="Top" DataKeyNames="PersonID"
        AllowSorting="true" EditFormSettings-EditColumn-Visible="false" EditFormSettings-ColumnNumber="2">
        <Columns>
            <other rows here>
            <telerik:GridTemplateColumn HeaderText="Role Code" DataField="RoleCode" SortExpression="RoleCode" EditFormColumnIndex="1">
                <ItemTemplate>
                    <asp:Label ID="lbl2" runat="server" Text='<%# Eval("RoleCode") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlRoleCode" runat="server">
                        <asp:ListItem>RTR</asp:ListItem>
                        <asp:ListItem>INT</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns
GoldAnchor
  • 129
  • 4
  • 13
  • These are server tags in asp.net, here's a explanation http://stackoverflow.com/questions/957284/whats-the-deal – Dmitry Sadakov Feb 25 '14 at 22:44
  • It's a databinding expression. It means that `lbl2.Text` will be assigned to the value of the `RoleCode` property of the object being databound. See more at http://msdn.microsoft.com/en-us/library/ms178366.ASPX – John Gibb Feb 25 '14 at 22:44
  • By the way, there's some aspx magic going on here. `Eval` is actually calling something like (from memory) `DataBinder.Eval(container.DataItem, "RoleCode")` – John Gibb Feb 25 '14 at 22:46

1 Answers1

2

The <% %> tags are code tags in ASP.NET. They let you run C# (or VB.NET) code within the markup page.

When a # is present, it means it's a binding expression. Within a control that performs data binding, such as the RadGrid you reference, the <%# %> tag (along with the Eval method) allow you to bind data from the current row to the controls within the grid.

So in your example, for each row in the data collection you're binding, text of the lbl2 Label gets set to the value of the RoleCode column.

Dave Zych
  • 21,581
  • 7
  • 51
  • 66