tags:

views:

55

answers:

2

When I have a table in database with many rows and I want to display them by normal line (Not with Gridview). Like:

You teach these classes: class 1/1, class 2/1, class 3/1

we take (1/1 and 2/1 and 3/1) from database.

how can I do it ?

note: I use LINQ to deal with database.

+1  A: 

Using a repeater seems to be the easiest way to me. It would look something like this.

<div>
    You teach these classes:
    <asp:Repeater
        ID="rptListOfClasses" 
        runat="server" 
        DataSourceID="linqDataSource" >
        <ItemTemplate>
            <span> class <%# Eval("ClassDate") %>, </span>
        </ItemTemplate>
    </asp:Repeater>
</div>
Slim
A: 
<asp:ListView ID="lvClass" runat="server" DataSourceID="LinqDataSource1"  OnItemDataBound ="lvResult_ItemDataBound" >
    <LayoutTemplate>
        You teach these classes:&nbsp;
        <asp:PlaceHolder runat="server" ID="itemPlaceHolder" ></asp:PlaceHolder>
    </LayoutTemplate>
    <ItemTemplate>  
            <asp:Label ID="lblClass"  runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Class") + ", " %>' ></asp:Label>                                          
    </ItemTemplate>   

</asp:ListView>


<asp:LinqDataSource ID="LinqDataSource1"  runat="server" 
    ContextTypeName="MyDataContext" TableName="ClassList" 
    onselected="LinqDataSource1_Selected">
</asp:LinqDataSource>

Code Behind: //remove the last comma(,)

public class Test : System.Web.UI.Page
    {
        int iCount = 0;
 protected void lvResult_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {

                Label lblClass = e.Item.FindControl("lblClass") as Label;
                ListViewDataItem lvItem = e.Item  as ListViewDataItem ;
                if (lblClass != null)
                    if (lvItem.DataItemIndex == (iCount  - 1))
                        lblClass.Text = lblClass.Text.Substring(0, lblClass.Text.ToString().Length  - 2);



            }
        }

        protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {
            iCount = e.TotalRowCount;
        }

}
miti737