asp.net listview控件 - 在itemtemplate上使用动态列

时间:2011-06-23 21:15:16

标签: asp.net listview

向stackoverflow的聪明人致以问候!我有这个ListView与以下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>

现在我正在尝试使用动态字段名称...所以类似

<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>

但它不适合我。任何人都知道如何做到这一点?预先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您需要动态创建ItemTemplate。 Creating Web Server Control Templates Programmatically

答案 1 :(得分:0)

我写了一篇文章:http://start-coding.blogspot.com/2013/06/dynamic-columns-in-listview.html

在ItemDataBound事件上,执行以下操作:

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
                }
            }
        }
    }