简体   繁体   English

如何生成动态标签并使用列名和值作为文本

[英]How to generate dynamic labels and use the column name and value as the text

Is there any way, if it is possible to dynamically generate the ASP.net page from code-behind.有没有办法,如果可以从代码隐藏动态生成 ASP.net 页面。

Example:例子:

ASP.net: ASP.NET:

<div class="hidOverflow setFloatL smallPadLeft" style="width: 45%" runat="server" id="dvLeft">
</div>
<div class="hidOverflow setFloatL smallPadLeft" style="width: 45%" runat="server" id="dvRight">
</div>

Code-behind:代码隐藏:

using (SqlConnection conn = new SqlConnection(gloString))
{
    try
    {
        strQuery = @"";

        SqlDataAdapter da = new SqlDataAdapter(strQuery, conn);

        DataSet myDataSet = new DataSet();
        da.Fill(myDataSet);

        //dynamically generate label with the SQL column name as the Text
        //dynamically generate label with the SQL column value as the text
        //<div class="hidOverflow smallPad">
            //<div class="setFloatL halfWidth vertAlignT">
                //<span class="profileLabel">{SQL COLUMN NAME}</span>
            //</div>
            //<div class="setFloatL vertAlignT">
                //<asp:Label ID="lbl1" ClientIDMode="Static" runat="server" Text="{SQL COLUMN VALUE}"></asp:Label>
            //</div>
        //</div>
        //.. more .. stop at the 1/2 mark of the count for the dataset and add it to the "dvLeft" div

        // STOP...

        //dynamically generate label with the SQL column name as the Text
        //dynamically generate label with the SQL column value as the text
        //<div class="hidOverflow smallPad">
            //<div class="setFloatL halfWidth vertAlignT">
                //<span class="profileLabel">{SQL COLUMN NAME}</span>
            //</div>
            //<div class="setFloatL vertAlignT">
                //<asp:Label ID="lbl1" ClientIDMode="Static" runat="server" Text="{SQL COLUMN VALUE}"></asp:Label>
            //</div>
        //</div>
        //.. more .. continue from the 1/2 mark of the count for the dataset and add it to the "dvRight" div
    }
    catch (SqlException)
    {
    }
}

I am looking to make it dynamic so all I have to do is change the SQL query and the labels will be generated accordingly.我希望使其动态化,因此我所要做的就是更改 SQL 查询,并相应地生成标签。

I can most likely use a asp:Repeater control to achieve it?我最有可能使用asp:Repeater控件来实现吗?

You could try binding the repeater to the Datatable ColumnCollection:您可以尝试将转发器绑定到数据表 ColumnCollection:

private DataTable _dataTable;

public void LoadRepeater()
{
    //load dataset
    _dataTable = myDataSet.Tables[0];
    repeater.DataSource = _dataTable.Columns;
    repeater.DataBind();
}

public string GetColumnValue(string columnName)
{
    return _dataTable.Rows[0][columnName].ToString();
}

Then on the repeater:然后在中继器上:

<ItemTemplate>
   <div class="hidOverflow smallPad">
        <div class="setFloatL halfWidth vertAlignT">
            <span class="profileLabel"><%# Eval("ColumnName") %></span>
        </div>
        <div class="setFloatL vertAlignT">
            <asp:Label ID="lbl2" ClientIDMode="Static" runat="server" Text='<%# GetColumnValue(Eval("ColumnName")) %>'></asp:Label>
        </div>
  </div>
</ItemTemplate>

This will only work if you have a single row on your DataTable though.不过,这仅在您的 DataTable 上只有一行时才有效。

If you have more Rows, you may have to include an additional repeater for the row dimension.如果您有更多行,则可能需要为行维度包含一个额外的转发器。

------------------------------------------------------------------ -------------------------------------------------- ----------------

To Split the columns, you could do something like this (untested):要拆分列,您可以执行以下操作(未经测试):

private void LoadRepeater()
{
    //load dataset
    _dataTable = myDataSet.Tables[0];
    int columnCount = _dataTable.Columns.Count;
    int half = (int)columnCount/2;

    var columnCollection = _dataTable.Columns.OfType<DataColumn>();
    var firstHalfColumns = columnCollection.Take(half);
    var secondHalfColumns = columnCollection.Skip(half);

    repeater1.DataSource = firstHalfColumns;
    repeater1.DataBind();

    repeater2.DataSource = secondHalfColumns;
    repeater2.DataBind();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM