0

Client:

<asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table id="Table_car">
                    <tr>
                        <th>Pos#</th>
                        <th>Model</th>
                        <th>Price</th>
                        <th>Image</th>
                    </tr>
            </HeaderTemplate>

            <ItemTemplate>
                <tr>
                    <td><%# Eval("Pos#") %></td>
                    <td><%# Eval("Model") %></td>
                    <td><%# Eval("Price") %></td>
                    <td><%# Eval("Image ") %></td>
                </tr>
            </ItemTemplate>

Server:

protected void Page_Load(object sender, EventArgs e) { var table = new DataTable();

   try
   {
       using (var conn = new SqlConnection(_connectionString))
       {
           using (var cmd = new SqlCommand("spFilterByContinent", conn))
           {
               using (var adapter = new SqlDataAdapter(cmd))
               {
                   cmd.CommandType = CommandType.StoredProcedure;
                   adapter.Fill(table);
               }
           }
       }
       Repeater1.DataSource = table;
       Repeater1.DataBind();
   }
   catch (Exception exception)
   {
       //TODO: write excepetion 
   }
}

public void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        string header1 = string.Empty;
        string header2 = string.Empty;
        string header3 = string.Empty;
        string header4 = string.Empty;
        // Retrieve headers from database and assign to variables
        SetHeaderValue(e.Item, "litHeader1", header1);
        SetHeaderValue(e.Item, "litHeader2", header2);
        SetHeaderValue(e.Item, "litHeader3", header3);
        SetHeaderValue(e.Item, "litHeader4", header4);
    }
}

private void SetHeaderValue(RepeaterItem item, string litId, string headerText)
{
    var lit = item.FindControl(litId) as Literal;
    if (lit != null)
        lit.Text = headerText;
}
}

I am using a repeater for retrieving data from db,

my question is: How can I retrieve columns headers from db also, not just the data itself? <# Eval("COLUMN HEADER")%> <---alike

Eran Meir
  • 133
  • 2
  • 12

1 Answers1

1

Headers are not databound as the items of a repeater (see this question and its answers for details). But you can adjust the headers in the ItemCreated event of the Repeater. First, you'd need to place some literal controls in the headers and add a handler for the ItemCreated event:

<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
    <HeaderTemplate>
        <table id="Table_car">
            <tr>
                <th><asp:Literal ID="litHeader1" runat="server" /></th>
                <th><asp:Literal ID="litHeader2" runat="server" /></th>
                <th><asp:Literal ID="litHeader3" runat="server" /></th>
                <th><asp:Literal ID="litHeader4" runat="server" /></th>
            </tr>
        </HeaderTemplate>
    </HeaderTemplate>
    <!-- ... -->
</asp:Repeater>

Then you'd need to implement the event handler in the code behind file. Based on your question, this sample assumes that you want to use the column names of the data table that is assigned as the DataSource of the Repeater:

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
         var tbl = (DataTable)((Repeater)sender).DataSource;
         SetHeaderValue(e.Item, "litHeader1", tbl, 0);
         SetHeaderValue(e.Item, "litHeader2", tbl, 1);
         SetHeaderValue(e.Item, "litHeader3", tbl, 2);
         SetHeaderValue(e.Item, "litHeader4", tbl, 3);
    }
}

private void SetHeaderValue(RepeaterItem item, string litId, DataTable tbl, int colIndex)
{
    var lit = item.FindControl(litId) as Literal;
    if (lit != null)
    {
        string headerText = 
            tbl.Columns.Count > colIndex ? tbl.Columns[colIndex].ColumnName : "Not set";
        lit.Text = headerText;
    }
}
Community
  • 1
  • 1
Markus
  • 20,838
  • 4
  • 31
  • 55
  • doesn't work, maybe it related to the fact that the repeater don't have a data source yet? I'll add to the question the server code with your addition.. – Eran Meir Apr 08 '14 at 20:11
  • @user2487126: yes, ItemDataBound is raised when the Repeater is bound to data. So you need to set the `DataSource` of the Repeater and call `DataBind()`. – Markus Apr 08 '14 at 20:18
  • Thanks, can you tell me where to put those DataSource and bind commands? – Eran Meir Apr 08 '14 at 20:26
  • @user2487126: in your code sample you already set the `DataSource` and call `DataBind()`. But in the markup, the registration of the `ItemDataBound` event is missing (`OnItemDataBound="Repeater1_ItemDataBound"`). Please verify this and check in the debugger whether the event handler is called. – Markus Apr 08 '14 at 20:35
  • @user2487126: you don't have to. To avoid a misunderstanding: first load the table, assign the `DataSource` and call `DataBind()`. Your server code looks correct, but the registration of the event handler in the aspx is missing: `` – Markus Apr 08 '14 at 20:38
  • sorry, I don't understand why isn't it work, this is the registration of the event handler: , and as for the datasource and the data bind- i call them in the PageLoad function.. – Eran Meir Apr 08 '14 at 20:45
  • @user2487126: hmm, the registration is also there. Strange that it doesn't work. Did you check in the debugger whether the event handler is called? – Markus Apr 08 '14 at 20:49
  • yes, I checked it. it is called. Do you have other idea? – Eran Meir Apr 08 '14 at 20:51
  • @user2487126: hmm, just fixed something in my sample in `SetHeaderValue`: I changed `as Literal` to `as LiteralControl`. Sorry, hope this fixes the problem. – Markus Apr 08 '14 at 20:54
  • in debug I saw that the function is been called and it calls with data.. But " SetHeaderValue(e.Item, "litHeader1", header1);", e.item doesn't contain nothing.. – Eran Meir Apr 08 '14 at 20:55
  • and I changed Literal to LiteralControl and it's the same – Eran Meir Apr 08 '14 at 20:57
  • @user2487126: if `e.Item` is null, you'd get an exception. Do you get an exception? Do the IDs of the LiteralControls in the aspx and the code match? Is `lit` set after the call to `FindControl`? – Markus Apr 08 '14 at 21:01
  • @user2487126 - I think you did not populate `header1`, `header2` etc with the value from database...the line `// Retrieve headers from database and assign to variables` is the place where you have to have code to do that. – afzalulh Apr 08 '14 at 21:04
  • I don't get an exception, the IDs match, the "headerText" variable in the "SetHeaderValue" function is empthy.. – Eran Meir Apr 08 '14 at 21:09
  • I don't understand something, SetHeaderValue(e.Item, "litHeader1", header1); - the call to "SetHeaderValue" with the parameter "header1", where does the function fill header1 with data? – Eran Meir Apr 08 '14 at 21:10
  • @user2487126: you need to handle `ItemCreated`instead of `ItemDataBound`. I couldn't get `FindControl` to work in `ItemDataBound` for a header, but in `ItemCreated` it works. Also, you need to change `LiteralControl` back to `Literal`. – Markus Apr 08 '14 at 21:19
  • Did You check it? and changed to "Literal" also.. and it's the same as before:) – Eran Meir Apr 08 '14 at 21:25
  • @user2487126: yes, I've checked it and it works. Can you check in the debugger whether `lit` is set in `SetHeaderValue` now? – Markus Apr 08 '14 at 21:28
  • no, its not.. same as before "headerText" is empthy – Eran Meir Apr 08 '14 at 21:31
  • @user2487126: do you retrieve the values from the database? Are the values of `header1` to `header4` set? Do the IDs of the Literals (`litHeader1` to `litHeader4` in my sample) match the ones you use in the code? – Markus Apr 08 '14 at 21:32
  • the values are not set, this is the question I asked you before " I don't understand something, SetHeaderValue(e.Item, "litHeader1", header1); - the call to "SetHeaderValue" with the parameter "header1", where does the function fill header1 with data? " I didn't change the IDs of the literals.. litHeader1- client and litHeader1-in server. etc' – Eran Meir Apr 08 '14 at 21:37
  • @user2487126: you need to set the values of `header1` to `header4` to the text you want to show in the Repeater. If you want to retrieve the column headers from a database, substitute the comment in my sample with the code that reads the texts from the database. – Markus Apr 08 '14 at 21:39
  • OK, this was the problem from start:) how do I retrieve the header from db? – Eran Meir Apr 08 '14 at 21:42
  • @user2487126: depends on how you store it. Do you want to use the column headers of your datatable that is the `DataSource` of your Repeater? – Markus Apr 08 '14 at 21:43
  • @user2487126: I've updated my sample and changed the code of `Repeater1_ItemCreated` and `SetHeaderValue`. – Markus Apr 08 '14 at 21:52