Jviaches
Jviaches

Reputation: 843

Define Datalist HeaderTemplate in c# from code behind

I wrote following code in aspx, and the problem is that i need to create same datalist from code behind in C#:

<asp:DataList ID="DataList1" runat="server">
    <HeaderTemplate>
        <tr>
            <th style="background-color: Black;">
                <div style="color: White; font-size: medium; padding: 0; margin: 0;">
                    TEST</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    date</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    Buy/Sell</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    Call/Put</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    name</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    amount</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    price1</div>
            </th>
            <th>
                <div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
                    price2</div>
            </th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <div>
            <tr>
                <td>
                    <div style="background-color: #71B24C; color: White; height: 50px; text-align: center;">
                        <%# Eval("option")%></div>
                </td>
                <td>
                    <div style="background-color: #ADAAB1; color: White; height: 50px; padding-right: 5px;
                        font-size: medium; text-align: center;">
                        <%# Eval("date")%></div>
                </td>
                <td>
                    <div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
                        text-align: center;">
                        <%# Eval("type")%></div>
                </td>
                <td>
                    <div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
                        text-align: center;">
                        <%# Eval("action")%></div>
                </td>
                <td>
                    <div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
                        text-align: center;">
                        <%# Eval("pos_name")%></div>
                </td>
                <td>
                    <div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
                        text-align: center;">
                        <%# Eval("amount")%></div>
                </td>
                <td>
                    <div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
                        text-align: center;">
                        <%# Eval("unitPrice")%></div>
                </td>
                <td>
                    <div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
                        text-align: center;">
                        <%# Eval("total")%></div>
                </td>
            </tr>
        </div>
    </ItemTemplate>
</asp:DataList>

I mean that code that i demonstrating works perfectly in aspx page. I am interesting in getting same effect (display datalist) but through the code behind. I mean to code in code behind something like : DataList = new DataList(); My problem is, i dont know how to work with HeaderTemplate within code behind. If you can provide example code, it would be nice!

Upvotes: 2

Views: 7611

Answers (2)

Jared Harley
Jared Harley

Reputation: 8337

I don't have any experience with DataLists, but from looking over the MSDN documentation it looks like what you'll want to do is bind your data source to the DataList in your code-behind and then use asp controls on the aspx page to display the bound data.

So, you'll want to bind your data to the list:

using (conn = new SqlConnection(cString)) {
    conn.Open();
    comm = new SqlCommand(selString, conn);
    DataList1.DataSource = comm.ExecuteReader();
    DataList1.DataBind();
    }

and then format your DataList to load data from the bound source like you've already done, with statements like <%# Eval("date")%>.

MSDN also has a page on how to create ASP.NET Server controls:

1. In the .aspx file, insert an element inside the control to identify what template you are creating, as in the following example:
<asp:DataList id="DataList1" runat="server">
  <ItemTemplate>
  </ItemTemplate> </asp:DataList>
2. Inside the template element, add HTML text and other controls as the template's content. Include property and data-binding values for the embedded controls using normal syntax, as in the following example:
<asp:DataList id="DataList3" runat="server">
  <ItemTemplate>
   Name: <asp:Label ID="Label2" runat="server" 
   Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'/>
  </ItemTemplate>
</asp:DataList>
3. Repeat Steps 1 and 2 for each template you want to create.

Upvotes: 0

Related Questions