Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:05:51+00:00 2026-05-27T20:05:51+00:00

I’ve been trying to figure out a way of presenting data dynamically but have

  • 0

I’ve been trying to figure out a way of presenting data dynamically but have been having no luck.
I have an alert system (which consists of a SQL Server DB table) and then I want that for each (relevant) row in that table, to construct a string to be presented in the ASP page, and also to insert a checkBox (just so I can mark the alert as “read”).

I already have the data and all (I think I’m doing it correctly) but have been completely unable to present this data on the page in the way I desire.

Here is the C# code:

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();
    }
}

And here is the ASP page where I want to present the data:

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

The objective is to be able to present the constructed string to the page, and for each of these lines, also provide a CheckBox which will have the same ID as the alertID (on the DB) just for the sake of simplicity of marking them as read.

I’m really lost here, and could use some help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:05:51+00:00Added an answer on May 27, 2026 at 8:05 pm

    You could add the items dynamically. However, this is a bit of a pain. You have to add them each time, not just the first time the page loads. Also, you need to add them early in the page lifecycle (e.g. in the Page_Init event) if you want the viewstate to be preserved between postbacks and to remember which boxes were checked.

    A simpler approach would be to use a control such as the ListView. In ASP.NET 4.0 you can easily get the control names exactly as you want them, but you’ll probably be OK with the auto-generated control names in ASP.NET 3.5. Here’s an example of what the ASPX page might look like:

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

    And here’s some sample code-behind. Note that I’ve replaced the data access code with a hard-coded list. I assume you’re comfortable replacing this with the code required to load the data from your database.

        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);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.