I have a Gridview bound to a linqdatasource. The gridview has a FK. i want to display a name/text field instead of the key field for this column.
I have created a method in my ASP.NET page that basically GetLookupForKey that returns the string when provided the key. However, I don't know how to send the column data for the specific row in the data declaration.
This should make it clear:
<asp:TemplateField> <ItemTemplate> <asp:Literal ID="RoleName" runat="server" Text='<%#
GetRoleName(* I need to send in the RoleId Here*) %>' />
RoleId is both a bou开发者_高级运维ndfield and a DataKeyName. How can I send the RoleId to my method? Additionally, how can I achieve this without using any codebehind?
Thanks
I can see two ways right now, maybe there are even more.
1) With code behind
<asp:Literal ID="RoleName" runat="server" Text='<%# GetRoleName(Eval("RoleId")) %>'/>
This one will require a protected method GetRoleName(object roleId)
in the page's code behind class.
2) Without code behind
I assume that objects Role
and whatever object is referencing it are both declared in the Linq context. If so, Linq can (and even does it by default behavour) generate properties for referenced objects. That is, when you have a table with FK to Role
, the corresponding object will have both RoleID
and Role
properties. Therefore everything can be accomplished declaratively:
<asp:Literal ID="RoleName" runat="server" Text='<%# Eval("Role.Name") %>'/>
精彩评论