如何产生用于HiddenField的每个值的表? [英] How to generate a table for each value of the HiddenField?

查看:201
本文介绍了如何产生用于HiddenField的每个值的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发有一个庞大的数据库的Web应用程序。我在我的数据库下表:

I am developing a web application that has a huge database. I have the following tables in my database:

组表:组ID,组名

课程表:CourseID,课程名,组ID

Course Table: CourseID, CourseName, GroupID

用户表:用户名,姓名,工作

User Table: Username, Name, Job

User_Course表:用户名,CourseID

User_Course Table: Username, CourseID

的第一个属性是每个表中的主键除了最后表

The first attribute is the primary key in each table except for the last table.

现在,我想创建一个表的基础上组ID的价值每种类型的课程。我有三个群体的课程。我使用的是占位符,我用它给HTMLTABLE。我用这个方法的灵活性,因为我有一个不能被其它控件,如GridView和所以没有。

Now, I am trying to create a table for each type of courses based on the value of GroupID. I have three groups of courses. I am using a PlaceHolder that I am using it to the HtmlTable. I used this method for flexibility because I have many complicated things which can't be done by the other controls like GridView and so no.

反正我是能够创建表,一切运作良好,并罚款。现在,我要生成一个表的基础上组ID的值是HiddenField各组的课程。

Anyway, I could be able to create the table and everything works well and fine. Now, I want to generate a table for each group of course based on the value of GroupID which is the HiddenField.

我的code:

<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" />

和我的code-背后:

and my code-Behind:

//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);

我知道,上面的code应该是一个循环中,但我也没怎么想出这个循环。

I know that the above code should be inside a loop, but I did not how to come up with this loop.

推荐答案

不要在$ C $使用的SqlDataSource 结果C-身后,把它控制端数据绑定。

Don't use SqlDataSource result in code-behind, leave it for control-side databinding.

使用纯ADO.NET对象,而不是:的SqlConnection,SqlCommand的,SqlDataReader的,例如:

Use pure ADO.NET object instead: SqlConnection, SqlCommand, SqlDataReader, e.g.:

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"];
        }
    }
}

这篇关于如何产生用于HiddenField的每个值的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆