[英]How to generate a table for each value of the HiddenField?
我正在开发一个具有庞大数据库的Web应用程序。 我的数据库中有以下表格:
组表:GroupID,GroupName
课程表:CourseID,CoursName,GroupID
用户表:用户名,名称,作业
User_Course表:用户名,CourseID
除最后一个表外,第一个属性是每个表中的主键。
现在,我试图根据GroupID的值为每种类型的课程创建一个表格。 我有三组课程。 我正在将PlaceHolder用于HtmlTable。 我使用此方法具有灵活性,因为我有很多复杂的东西,而GridView等其他控件无法完成,所以没有。
无论如何,我可以创建表,并且一切正常。 现在,我想根据GroupID的值HiddenField为每个课程组生成一个表。
我的代码:
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommandType="StoredProcedure" SelectCommand="kbiReport">
<SelectParameters>
<%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values of GroupID--%>
<%--<asp:Parameter Name="GroupID" DefaultValue="3" />--%>
<asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="updateButton" runat="server" OnClick="updateButton_Click" Text="Update" />
和我的代码背后:
//create a new HtmlTable object
HtmlTable table = new HtmlTable();
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
int columns = dv.Table.Columns.Count;
int rows = dv.Count;
//table's formating-related properties
table.Border = 2;
table.CellPadding = 3;
table.CellSpacing = 3;
//to get the css style
table.Attributes["class"] = "mGrid";
//create a new HtmlTableRow and HtmlTableCell objects
HtmlTableRow row;
HtmlTableRow header = new HtmlTableRow();
HtmlTableCell cell;
foreach (DataColumn column in dv.Table.Columns)
{
HtmlTableCell headerCell = new HtmlTableCell("th");
headerCell.InnerText = column.Caption;
header.Cells.Add(headerCell);
}
table.Rows.Add(header);
//loop for adding 5 rows to the table
foreach (DataRowView datarow in dv)
{
row = new HtmlTableRow();
row.BgColor = "yellow";
for (int j = 0; j < columns; j++)
{
cell = new HtmlTableCell();
if (j < 4)
{
cell.InnerText = datarow[j].ToString();
}
else
{
CheckBox checkbox = new CheckBox();
int checkBoxColumns = dv.Table.Columns.Count - 5;
string fieldvalue = datarow[j].ToString();
string yes = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[1];
string courseid = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0];
checkbox.ID = row.Cells[3].InnerText + "," + courseid.Trim();
checkbox.Checked = yes.Equals("Yes");
cell.Controls.Add(checkbox);
}
//add the cell to the current row
row.Cells.Add(cell);
}
//add the row to the table
table.Rows.Add(row);
}
//add the table to the page
PlaceHolder1.Controls.Add(table);
我知道上面的代码应该在一个循环中,但是我没有如何提出这个循环。
不要在代码后面使用SqlDataSource
结果,将其留给控制端数据绑定。
改用纯ADO.NET对象:SqlConnection,SqlCommand,SqlDataReader,例如:
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = commandText;
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int id = (int)reader["ID"];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.