我希望以表格格式显示每个数据库值。
答案 0 :(得分:0)
考虑到您的问题,我有一个使用Repeater控件的解决方案。 请查看下面的代码段,其中我使用repeater显示了Month。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="rptCalender">
<HeaderTemplate>
<table style="height: auto; width: 450px; border: 1px solid black">
<tr>
<td align="center">
<span style="font-weight: bold;">Month Name</span>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:Label ID="lblMonthName" runat="server" Text='<%# Eval("MonthName")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
背后的代码如下,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindCalenderData();
}
void BindCalenderData()
{
string[] monthList = DateTimeFormatInfo.CurrentInfo.MonthNames;
DataTable dtCalenderData = new DataTable();
dtCalenderData.TableName = "Calender";
dtCalenderData.Columns.Add("MonthName", typeof(string));
int totalMonth = monthList.Length;
for (int counterMonth = 0; counterMonth < totalMonth; counterMonth++)
{
if (!string.IsNullOrEmpty(monthList[counterMonth]))
{
DataRow drRowData = dtCalenderData.NewRow();
drRowData["MonthName"] = monthList[counterMonth];
dtCalenderData.Rows.Add(drRowData);
}
}
rptCalender.DataSource = dtCalenderData;
rptCalender.DataBind();
}
我希望这能解决您的问题。