Question

I am using DataList and in one column I need to display two fields combined, I need the column display as Name1(Name2)

Here is my code:

<asp:DataList ID="dlistDetails" runat="server" DataKeyField="KeyID"
              RepeatColumns="3" Width="100%" 
              AlternatingItemStyle-BackColor="Wheat" CaptionAlign="Top">
    <ItemTemplate>
        <asp:Label ID="lbl" runat="server" Text= '<%#Eval("Name1")%(<%#Eval("Name2") %>)' /> 
    </ItemTemplate>
</asp:DataList>

The same is done in my GridView and its showing the result,

Was it helpful?

Solution

Just tweak your existing code to:

<asp:DataList ID="dlistDetails" runat="server" DataKeyField="KeyID"
              RepeatColumns="3" Width="100%" 
              AlternatingItemStyle-BackColor="Wheat" CaptionAlign="Top">
    <ItemTemplate>
        <asp:Label ID="lbl" runat="server" Text='<%# Eval("Name1") + "(" +  Eval("Name2") + ")"%>' /> 
    </ItemTemplate>
</asp:DataList>

OTHER TIPS

You can try with this code

<ItemTemplate>
        <asp:Label ID="lbl" runat="server" Text='<%# String.Format(
      "return {0} % {1}", 
       Eval("Name1"), 
       Eval("Name2")) %>' /> 

    </ItemTemplate>

I have a easiest way to do this same thing...

<asp:Label ID="lbl" runat="server" Text='<%#Eval("Name1").ToString() +", "+ Eval("Name2").ToString() %>'></asp:Label>

.

<%#Eval("Name1").ToString() +", "+ Eval("Name2").ToString() %>

Here both objects are converted into string the concatenate them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top