C# 如何生成动态标签并将列名和值用作文本
如果可以从代码隐藏动态生成ASP.net页面,是否有任何方法 例如: ASP.net:C# 如何生成动态标签并将列名和值用作文本,c#,asp.net,sql-server,C#,Asp.net,Sql Server,如果可以从代码隐藏动态生成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="dvR
<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>
代码隐藏:
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)
{
}
}
使用(SqlConnection conn=newsqlconnection(gloString))
{
尝试
{
strQuery=@”;
SqlDataAdapter da=新的SqlDataAdapter(strQuery,conn);
DataSet myDataSet=新数据集();
da.Fill(myDataSet);
//以SQL列名作为文本动态生成标签
//以SQL列值作为文本动态生成标签
//
//
//{SQL列名}
//
//
//
//
//
//..更多..在数据集计数的1/2处停止,并将其添加到“dvLeft”div
//停止。。。
//以SQL列名作为文本动态生成标签
//以SQL列值作为文本动态生成标签
//
//
//{SQL列名}
//
//
//
//
//
//..更多..从数据集计数的1/2标记继续,并将其添加到“dvRight”div
}
捕获(SqlException)
{
}
}
我希望它是动态的,所以我所要做的就是更改SQL查询,并相应地生成标签
我最有可能使用
asp:Repeater
控件来实现它?您可以尝试将Repeater绑定到Datatable 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();
}
然后在中继器上:
<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>
但是,这仅在DataTable上只有一行时有效
如果有更多行,则可能需要为行维度添加一个额外的中继器
------------------------------------------------------------------
要拆分列,可以执行以下操作(未测试):
private void loader repeater()
{
//加载数据集
_dataTable=myDataSet.Tables[0];
int columnCount=\u dataTable.Columns.Count;
int half=(int)columnCount/2;
var columnCollection=_dataTable.Columns.OfType();
var firstHalfColumns=columnCollection.Take(half);
var secondHalfColumns=columnCollection.Skip(一半);
repeater1.DataSource=firsthalf列;
repeater1.DataBind();
repeater2.DataSource=secondhalf列;
repeater2.DataBind();
}
谢谢。它将始终是一行或没有行。我会测试一下并更新。你觉得我的解决方案怎么样?太棒了。只需将数据集按列一分为二,并在两个单独的分区中使用。感谢您的指导:)您能让我知道如何按列数将数据表拆分为两个单独的DT…@SearchForKnowledge我用一个建议更新了答案,不确定它是否是您所需要的。Scheiders您让我免于编写许多查询。谢谢:)
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();
}