【发布时间】:2015-07-21 18:18:51
【问题描述】:
我想知道是否可以使用 asp 中的循环写入 asp:table 的特定单元格。
例如
ASPx 文件
<table>
<tr>
<td>
<label>Week</label>
</td>
<td>
<asp:DropDownList runat="server" ID="lecWeekDD">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
<asp:ListItem Text="5" Value="5" />
<asp:ListItem Text="6" Value="6" />
<asp:ListItem Text="7" Value="7" />
<asp:ListItem Text="8" Value="8" />
<asp:ListItem Text="9" Value="9" />
<asp:ListItem Text="10" Value="10" />
<asp:ListItem Text="11" Value="11" />
<asp:ListItem Text="12" Value="12" />
</asp:DropDownList>
</td>
</tr>
</table>
<<asp:Table runat="server" ID="lecTimetable">
<asp:TableRow>
<asp:TableCell Text="Week/Period" />
<asp:TableCell Text="P1 (09:00-10:00)"/>
<asp:TableCell Text="P2 (10:00-11:00"/>
<asp:TableCell Text="P3 (11:00-12:00)"/>
<asp:TableCell Text="P4 (12:00-13:00"/>
<asp:TableCell Text="P5 (13:00-14:00)"/>
<asp:TableCell Text="P6 (14:00-15:00)"/>
<asp:TableCell Text="P7 (15:00-16:00)"/>
<asp:TableCell Text="P8 (16:00-17:00)"/>
<asp:TableCell Text="P9 (17:00-18:00)"/>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="Monday"/>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="Tuesday"/>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="Wednesday"/>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="Thursday"/>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="Friday"/>
</asp:TableRow>
</asp:Table>
以及代码背后的代码
public partial class Lecturer1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["currentUser"] != null)
{
// lbl1.Text = "Welcome " + Session["currentUser"].ToString();
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["UsersConnectionString"].ToString());
con.Open();
string day = "";
int period;
for (int i = 1; i <= 5; i++)
{
if(i==1){
day = "Monday";
}
else if (i == 2)
{
day = "Tuesday";
}
else if (i == 3)
{
day = "Wednesday";
}
else if (i == 4)
{
day = "Thursday";
}
else if (i == 5)
{
day = "Friday";
}
for (int j = 1; i <= 9; i++)
{
period = j;
SqlCommand command = new SqlCommand("SELECT [Request].moduleCode FROM [Request] INNER JOIN [BookedRoom] ON [Request].requestID = [BookedRoom].requestID INNER JOIN [Modules] ON [Request].moduleCode = [Modules].moduleCode INNER JOIN [Week] ON [Request].requestID = [Week].requestID WHERE [Week].week = "+lecWeekDD.SelectedValue.ToString()+" AND [Request].day = '"+day+"' AND [Request].periodStart = "+period+" AND (SELECT [ModuleLecturers].lecturerID FROM [ModuleLecturers] INNER JOIN [Modules] ON [ModuleLecturers].moduleCode = [Modules].moduleCode WHERE [Request].moduleCode = [Modules].moduleCode) = '"+Session["currentUser"]+"'" , con);
SqlDataReader reader;
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
lecTimetable[i][j].text;
//testBox.Text = reader.GetString(0);
}
}
else
{
//testBox.Text = "nothing found";
}
reader.Close();
}
}
}
是否可以通过将其 ID 与行和列(即lecTimetable [ i ] [ j ].text = .........)一起使用来写入表?
【问题讨论】:
-
您需要将结果分配给数据表 lecTimetable?
-
不是数据库表,只是普通表,例如 gridview 表,但别担心,我现在已经修复了
标签: c# asp.net loops html-table cells