Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Greetings smart people of stackoverflow! I have this ListView with the following itemtemplate

<ItemTemplate>
   <tr>
 <td><%#Eval("abc_availablearea").ToString()%>&nbsp;</td>
 <td><%#Eval("abc_classname").ToString() %>&nbsp;</td>
 <td><%#Eval("abc_division").ToString() %>&nbsp;</td>
 <td><%#Eval("abc_managername").ToString() %>&nbsp;</td>
   </tr>
</ItemTemplate>

Now I am trying to use dynamic field names...so something like

<ItemTemplate>
   <tr>
 <td><%#Eval(fieldOne).ToString()%>&nbsp;</td>
 <td><%#Eval(fieldTwo).ToString() %>&nbsp;</td>
 <td><%#Eval(fieldThree).ToString() %>&nbsp;</td>
 <td><%#Eval(fieldFour).ToString() %>&nbsp;</td>
   </tr>
</ItemTemplate>

But it's not working for me. Anyone have an idea on how to do this? Thank you in advance for your help.

share|improve this question
You could try to add the dynamic fields from the code behind in the ItemDataBound event, it would be more cleaner and easier to maintain. – Lucian Jun 23 '11 at 21:24
when you say "not working"...are you getting an error message? – Al W Jun 23 '11 at 21:25
Al...basically the error is that it doesn't know what fieldOne, etc is – DevilCode Jun 23 '11 at 22:00
Lucian - so what you are saying add a literal on the itemTemplate, then on the ItemDataBaound event construct the row? I think that will work! Thank you...I will try tomorrow or tonight...it's 5, time to go home lol! Thanks all for your help folks! – DevilCode Jun 23 '11 at 22:02

2 Answers

You'll need to create the ItemTemplate dynamically. Creating Web Server Control Templates Programmatically

share|improve this answer
Alison, that link is for a DataGrid control - but I dunno t might work on a Listview - will take a look - thank you. – DevilCode Jun 23 '11 at 22:04
@DevilCode: Correct. It's the same idea. You'll need to bind the fields programmatically in a similar fashion. You should be able to find some sample code online after a bit of searching. – Alison Jun 24 '11 at 1:53

I wrote a post on it: http://start-coding.blogspot.com/2013/06/dynamic-columns-in-listview.html.

On ItemDataBound event, do something like this:

private void dynamicPopulateRow(HtmlTableRow row, System.Data.DataRowView drv, int iGeneration)
    {
        if (row != null)
        {
            // http://www.pcreview.co.uk/forums/do-enumerate-all-columns-dataviewrow-t1244448.html
            foreach (DataColumn dc in drv.Row.Table.Columns)
            {
                string sEmployeeID = drv["LoginID"].ToString();

                if (dc.ColumnName.Equals("LoginID"))
                {
                    // http://msdn.microsoft.com/en-US/library/e5daxzcy(v=vs.80).aspx
                    // Define a new HtmlTableCell control.
                    HtmlTableCell cell = new HtmlTableCell("td");

                    // Create the text for the cell.
                    cell.Controls.Add(new LiteralControl(Convert.ToString(drv[dc.ColumnName])));
                    cell.ColSpan = dc.ColumnName.Equals("LoginID") ? I_COLSPAN - iGeneration : 1;

                    // Add the cell to the HtmlTableRow Cells collection. 
                    row.Cells.Add(cell);
                }
                else if (!(dc.ColumnName.Equals("GENERATION") ||
                            dc.ColumnName.Equals("hierarchy") ||
                            dc.ColumnName.Equals("rowNo") ||
                            dc.ColumnName.Equals("EmployeeID")))
                {
                    // http://msdn.microsoft.com/en-US/library/e5daxzcy(v=vs.80).aspx
                    // Define a new HtmlTableCell control.
                    HtmlTableCell cell = new HtmlTableCell("td");

                    bool bIsNull = drv[dc.ColumnName] is System.DBNull;

                    Literal ltrl = new Literal();
                    ltrl.Text += "<input type=\"checkbox\" name=\"" + dc.ColumnName + "\"" +
                                    (bIsNull ? "" : " value=" + drv[dc.ColumnName].ToString()) +
                                    " id=\"" + sEmployeeID + "~" + dc.ColumnName.Replace(" ", "_") + "\"" +//will be retrieved later
                                    " onclick=\"didModify(this)\" " +
                                    (bIsNull ? " disabled" : "") +
                                    (!bIsNull && ((int)drv[dc.ColumnName]) > 0 ? " checked>" : ">");

                    cell.Controls.Add(ltrl);
                    // Add the cell to the HtmlTableRow Cells collection. 
                    row.Cells.Add(cell);
                }
                else
                {
                    //other rows
                }
            }
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.