Skip to content
Advertisement

Asp.Net adding an extra td

I’m trying to use an ASP:DataList to display data from my data source. Everything works ok, except I’m getting ASP to add an extra column when there isn’t one. The Header Template and Item Template are shown below

<asp:DataList runat="server" ID="xTable" DataKeyField="PK_PurchaseID" DataSourceID="SqlDataSource1" RepeatLayout="table" CssClass="table">
        <HeaderTemplate>
            <td>PuchaseID</td>
            <td>Last Name</td>
            <td>First name</td>
            <td>Address1</td>
            <td>Email Sent</td>
        </HeaderTemplate>
        <ItemTemplate>
            <td><%# Eval("PK_PurchaseID") %></td>
            <td><%# Eval("LastName") %></td>
            <td><%# Eval("FirstName") %></td>
            <td><%# Eval("Address1") %></td>
            <td><%# Eval("[Email Sent]") %></td>
        </ItemTemplate>
    </asp:DataList>

And is the the DOM object created. I would expect 5 columns in the table yet it is rendered as 6

<table class="table" id="xTable" style="border-collapse: collapse;" cellspacing="0">
<tbody><tr>
    <td>
            <td>PuchaseID</td>
            <td>Last Name</td>
            <td>First name</td>
            <td>Address1</td>
            <td>Email Sent</td>

</tr><tr>
    <td>
            <td>3</td>
            <td>Albdddim</td>
            <td>James </td>
            <td>1----63rd Ave.</td>
            <td>true</td>

</tr>

I can not figure you why an initial column is being created. I changed the CSS, even removed it and still the same result. I have verified the SQL only returns the 5 columns (even if it returned more or less that shouldn’t make a difference)

Thanks Chris

Advertisement

Answer

The HeaderTemplate and ItemTemplate are meant to be td elements so it automatically creates the td. If you do something like this

<asp:DataList runat="server" ID="xTable" DataKeyField="PK_PurchaseID" DataSourceID="SqlDataSource1" RepeatLayout="table" CssClass="table">
    <HeaderTemplate>
        PuchaseID
        <td>Last Name</td>
        <td>First name</td>
        <td>Address1</td>
        <td>Email Sent</td>
    </HeaderTemplate>
    <ItemTemplate>
        <%# Eval("PK_PurchaseID") %>
        <td><%# Eval("LastName") %></td>
        <td><%# Eval("FirstName") %></td>
        <td><%# Eval("Address1") %></td>
        <td><%# Eval("[Email Sent]") %></td>
    </ItemTemplate>
</asp:DataList>

It would put the purchase ID in the first td that is created and then fill out the table like you want.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement