ASP中数据的动态表示.网络应用程序

本文关键字:表示 网络 应用程序 动态 数据 ASP | 更新日期: 2023-09-27 18:18:58

我一直在试图找出一种动态呈现数据的方法,但一直没有运气。我有一个警报系统(由一个SQL Server DB表组成),然后我想要在该表中的每个(相关)行,构建一个字符串在ASP页面中呈现,也插入一个复选框(只是这样我就可以将警报标记为"读取")。

我已经有了数据和所有(我认为我做的是正确的),但已经完全无法以我想要的方式在页面上呈现这些数据。

下面是c#代码:
protected void getAlertas(string matricula)
{
    string query = "SELECT * FROM ALERTAS A ";
    query += "WHERE EXISTS ( ";
    query += "SELECT ID, MATRICULA FROM VEICULOS V WHERE V.MATRICULA = @matricula ";
    query += "AND A.IDVEICULO = V.ID ";
    query += "AND LIDA = 0)";
    SqlCommand comm = new SqlCommand(query, Utils.connectDB());
    matricula = matricula.ToString().Replace("'"", "'");
    comm.Parameters.AddWithValue("@matricula", matricula);
    StringBuilder builder = new StringBuilder();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter();
    StringCollection alertValues = new StringCollection();
    StringBuilder HTML = new StringBuilder();
    try
    {
        comm.Connection.Open();
        SqlDataReader reader = comm.ExecuteReader();
        dt.Load(reader);
        foreach (DataRow row in dt.Rows)
        {
            Label label = new Label();
            CheckBox cBox = new CheckBox();
            foreach (DataColumn column in dt.Columns)
            {
                alertValues.Add(row[column].ToString());
            }
            builder.Append(alertValues[0]); //ID do alerta
            builder.Append(":");
            builder.Append(matricula);
            if (int.Parse(alertValues[10]) == 0) //se motor desligado
            {
                //string output =  "Motor desligado às " + alertValues[3] + "do dia " + alertValues[4];
                builder.Append("Motor desligado às ");
            }
            else if(int.Parse(alertValues[10]) == 1) //se motor ligado
            {
                //string output = "Motor ligado às " + alertValues[3] + "do dia " + alertValues[4];
                builder.Append("Motor ligado às ");
            }
            builder.Append(alertValues[3]); //hora
            builder.Append("do dia ");
            builder.Append(alertValues[4]); //data
            //HTML.Append(" <div id='"notifications'" runat='"server'" class='"notifications'">");
            //HTML.Append(" <div class='"notificationText'">");
            //HTML.Append("<asp:Label ID='"Label1'" runat='"server'" Text='"" + builder.ToString() + "'"></asp:Label>");
            //HTML.Append("</div>");
            //HTML.Append("<div class='"setRead'">");
            //HTML.Append("<asp:CheckBox ID='"" + alertValues[0] + "'" runat='"server'" />");
            //HTML.Append("</div>");
            //HTML.Append("</div>");

            label.Text = builder.ToString();
            cBox.ID = alertValues[0];
        }
    }
    finally
    {
        comm.Connection.Close();
    }
}

这是我要展示数据的ASP页面:

<div id="contentRight">
        <div id="head">
            <p>
                <b>Notificações</b></p>
        </div>
        <div id="notifications" runat="server" class="notifications">
            <div class="notificationText">
                <asp:Label ID="Label1" runat="server" Text="Nada para mostrar"></asp:Label>
            </div>
            <div class="setRead">
                <asp:CheckBox ID="AlertIDRead" runat="server" />
            </div>
        </div>
        <hr />
        <div id="OkBtn" align="center">
            <asp:Button ID="OkButton" class="Button" runat="server" Text="Ok" />
        </div>
    </div>

目标是能够将构造的字符串呈现给页面,并且对于每一行,还提供一个CheckBox,该CheckBox将具有与alertID(在DB上)相同的ID,只是为了简单地将它们标记为已读。

我真的迷路了,需要一些帮助。

ASP中数据的动态表示.网络应用程序

您可以动态地添加条目。然而,这有点痛苦。您必须每次都添加它们,而不仅仅是第一次加载页面。此外,如果你想在回发之间保留视图状态并记住哪些框被选中,你需要在页面生命周期的早期添加它们(例如在Page_Init事件中)。

一个更简单的方法是使用一个控件,比如ListView。在ASP。你可以很容易地得到你想要的控件名,但是你可能会对ASP中自动生成的控件名感到满意。NET 3.5。下面是一个ASPX页面的示例:

<asp:ListView runat="server" ID="listView" DataKeyNames="AlertId">
    <LayoutTemplate>
        <div id="contentRight">
            <div id="head">
                <p>
                    <b>Notificações</b></p>
            </div>
            <div id="notifications" runat="server" class="notifications">
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            </div>
        </div>      
    </LayoutTemplate>
    <ItemTemplate>
        <div class="notificationText">
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("AlertText")%>' />
        </div>
        <div class="setRead">
            <asp:CheckBox ID="AlertChecked" runat="server" />
        </div>
    </ItemTemplate>
</asp:ListView>
<hr />
<div id="OkBtn" align="center">
    <asp:Button ID="OkButton" class="Button" runat="server" Text="Ok" onclick="OkButton_Click" />
</div>
<asp:Label runat="server" ID="lblDisplay" />

下面是一些示例代码。注意,我已经用硬编码列表替换了数据访问代码。我假设您可以将其替换为从数据库加载数据所需的代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Creates a list of alerts and binds them to the list view.
            // You would use your sql data reader here instead.  You could convert the data
            // into a list or just bind the data reader directly to the list view.  If you
            // bind the data reader to the list view then be sure to update the Eval statements
            // in the list view templates.
            var alerts = new[]
            {
                new { AlertId = 1, AlertText = "This is alert #1."},
                new { AlertId = 2, AlertText = "This is the second alert."},
                new { AlertId = 3, AlertText = "This is the third alert."},
                new { AlertId = 5, AlertText = "This is alert #5."}
            };
            listView.DataSource = alerts;
            listView.DataBind();
        }
    }
    protected void OkButton_Click(object sender, EventArgs e)
    {
        // get the list of alerts that have been checked
        var checkedAlerts = new List<int>();
        foreach (var listViewItem in listView.Items)
        {
            var checkbox = (CheckBox)listViewItem.FindControl("AlertChecked");
            if (checkbox.Checked)
            {
                checkedAlerts.Add((int)listView.DataKeys[listViewItem.DataItemIndex].Value);
            }
        }
        // now do something to record them as read; we'll just display them in a label for this sample.
        lblDisplay.Text = "The following alerts were marked read: " + String.Join(",", checkedAlerts);
    }