Can we create DataList Row Headers???

Can we create DataList Row Headers???

Post by J 2 the » Thu, 27 Mar 2003 10:18:06



I am showing Data using a DataList, with RepeatDirection set as
Horizontal.

The data is rendering ok, but I need to show a Header name in the
first cell of each row. I am unable to do this using DataList, I can't
seem to get my Table to render correctly.

Has anyone done this? Can it be done?

I need to show the data horizontally, so changing the layout, or
changing to DataGrid is not an option.

You can cut and paste the code below (change the SQL login info) and
see what I am getting now:

Code Below
--------------------------------------



<html>
<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter

        MyConnection = New
SqlConnection("server=localhost;uid=test;pwd=test;database=pubs")
        MyCommand = New SqlDataAdapter("select * from Authors",
MyConnection)

        DS = new DataSet()
        MyCommand.Fill(ds, "Authors")

        MyDataList.DataSource=ds.Tables("Authors").DefaultView
        MyDataList.DataBind()
    End Sub

</script>

<body>

  <h3><font face="Verdana">DataList Test</font></h3>

<asp:DataList
ID="MyDataList"
Runat="server"
RepeatColumns="5"
RepeatDirection="Horizontal"
RepeatLayout="Table">

<HeaderTemplate>  
 <tr><td>ID
 </td></tr>
 <tr><td>Last Name
 </td></tr>
 <tr><td>First Name
 </td></tr>

</HeaderTemplate>

<ItemTemplate>

<tr>
<td><%# Container.DataItem("au_id") %></td>
</tr>
<tr>
<td><%# Container.DataItem("au_lname") %></td>
</tr>
<tr>
<td><%# Container.DataItem("au_fname") %></td>
</tr>

</ItemTemplate>

<FooterTemplate>
</FooterTemplate>
</asp:DataList>

</body>
</html>

------------------------------
End Code

Thanks in advance for any help

 
 
 

Can we create DataList Row Headers???

Post by Ben Miller [msft » Fri, 28 Mar 2003 17:06:42


How about this?  Look through the code and you will see that I added a
placeholder control in a cell in front of the Item that is being bound, and
it only gets used if it is the first one of the columns.

If you have any questions, please feel free to ask.

Copy and paste this and see if you get what you want.

-- Begin Code ---



<html>
<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter

        MyConnection = New
SqlConnection("server=localhost;uid=test;pwd=test;database=pubs")
        MyCommand = New SqlDataAdapter("select * from Authors",
MyConnection)

        DS = new DataSet()
        MyCommand.Fill(ds, "Authors")

        MyDataList.DataSource=ds.Tables("Authors").DefaultView
        MyDataList.DataBind()
    End Sub

    Sub DataList_ItemCreated(Sender as Object, E as DataListItemEventArgs)

 If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =
ListItemType.AlternatingItem Then
  If ((e.Item.ItemIndex Mod MyDataList.RepeatColumns) = 0) Then

   dim ph as PlaceHolder
   ph = CType(e.Item.FindControl("ID"), PlaceHolder)
   ph.Controls.Add(new LiteralControl("ID"))

   ph = CType(e.Item.FindControl("LName"), PlaceHolder)
   ph.Controls.Add(new LiteralControl("Last Name"))

   ph = CType(e.Item.FindControl("FName"), PlaceHolder)
   ph.Controls.Add(new LiteralControl("First Name"))

  End If
 End If
    End Sub

</script>

<body>

  <h3><font face="Verdana">DataList Test</font></h3>

<asp:DataList
ID="MyDataList"
OnItemCreated="DataList_ItemCreated"
Runat="server"
RepeatColumns="5"
RepeatDirection="Horizontal"
RepeatLayout="Table">

<ItemTemplate>

<table>
<tr>
<td><asp:PlaceHolder id="ID" runat="server" /></td><td><%#
Container.DataItem("au_id") %></td>
</tr>
<tr>
<td><asp:PlaceHolder id="LName" runat="server" /></td><td><%#
Container.DataItem("au_lname") %></td>
</tr>
<tr>
<td><asp:PlaceHolder id="FName" runat="server" /></td><td><%#
Container.DataItem("au_fname") %></td>
</tr>
</table>

</ItemTemplate>

<FooterTemplate>
</FooterTemplate>
</asp:DataList>

</body>
</html>

-- End Code ---

Ben.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

--


Quote:> I am showing Data using a DataList, with RepeatDirection set as
> Horizontal.

> The data is rendering ok, but I need to show a Header name in the
> first cell of each row. I am unable to do this using DataList, I can't
> seem to get my Table to render correctly.

> Has anyone done this? Can it be done?

> I need to show the data horizontally, so changing the layout, or
> changing to DataGrid is not an option.

> You can cut and paste the code below (change the SQL login info) and
> see what I am getting now:


 
 
 

Can we create DataList Row Headers???

Post by J 2 the » Sat, 29 Mar 2003 01:23:30


Ben -

Thanks so much, after weeks of pulling my hair out to make this work
you're example was exactly what I was looking for.

JB

----------------------------------------------------


> How about this?  Look through the code and you will see that I added a
> placeholder control in a cell in front of the Item that is being bound, and
> it only gets used if it is the first one of the columns.

> If you have any questions, please feel free to ask.

> Copy and paste this and see if you get what you want.

> -- Begin Code ---



> <html>
> <script language="VB" runat="server">

>     Sub Page_Load(Sender As Object, E As EventArgs)

>         Dim DS As DataSet
>         Dim MyConnection As SqlConnection
>         Dim MyCommand As SqlDataAdapter

>         MyConnection = New
> SqlConnection("server=localhost;uid=test;pwd=test;database=pubs")
>         MyCommand = New SqlDataAdapter("select * from Authors",
> MyConnection)

>         DS = new DataSet()
>         MyCommand.Fill(ds, "Authors")

>         MyDataList.DataSource=ds.Tables("Authors").DefaultView
>         MyDataList.DataBind()
>     End Sub

>     Sub DataList_ItemCreated(Sender as Object, E as DataListItemEventArgs)

>  If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =
> ListItemType.AlternatingItem Then
>   If ((e.Item.ItemIndex Mod MyDataList.RepeatColumns) = 0) Then

>    dim ph as PlaceHolder
>    ph = CType(e.Item.FindControl("ID"), PlaceHolder)
>    ph.Controls.Add(new LiteralControl("ID"))

>    ph = CType(e.Item.FindControl("LName"), PlaceHolder)
>    ph.Controls.Add(new LiteralControl("Last Name"))

>    ph = CType(e.Item.FindControl("FName"), PlaceHolder)
>    ph.Controls.Add(new LiteralControl("First Name"))

>   End If
>  End If
>     End Sub

> </script>

> <body>

>   <h3><font face="Verdana">DataList Test</font></h3>

> <asp:DataList
> ID="MyDataList"
> OnItemCreated="DataList_ItemCreated"
> Runat="server"
> RepeatColumns="5"
> RepeatDirection="Horizontal"
> RepeatLayout="Table">

> <ItemTemplate>

> <table>
> <tr>
> <td><asp:PlaceHolder id="ID" runat="server" /></td><td><%#
> Container.DataItem("au_id") %></td>
> </tr>
> <tr>
> <td><asp:PlaceHolder id="LName" runat="server" /></td><td><%#
> Container.DataItem("au_lname") %></td>
> </tr>
> <tr>
> <td><asp:PlaceHolder id="FName" runat="server" /></td><td><%#
> Container.DataItem("au_fname") %></td>
> </tr>
> </table>

> </ItemTemplate>

> <FooterTemplate>
> </FooterTemplate>
> </asp:DataList>

> </body>
> </html>

> -- End Code ---

> Ben.

> --
> This posting is provided "AS IS" with no warranties, and confers no rights.
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm

> --


> > I am showing Data using a DataList, with RepeatDirection set as
> > Horizontal.

> > The data is rendering ok, but I need to show a Header name in the
> > first cell of each row. I am unable to do this using DataList, I can't
> > seem to get my Table to render correctly.

> > Has anyone done this? Can it be done?

> > I need to show the data horizontally, so changing the layout, or
> > changing to DataGrid is not an option.

> > You can cut and paste the code below (change the SQL login info) and
> > see what I am getting now: