Establish a Zebra DataList Chart

My first task is to connect a Table from the database so that each row is editable. After some research, I settled on using the DataList due to its <EditItemTemplate> property, which the repeater lacks.

To create a Zebra table, I crafted a CSS class like so:

.row:nth-of-type(odd)
{
background-color:Green;
}

When I applied this class to the repeater, it worked smoothly since the repeater allows for coding rows and columns individually. Here is an example:

<asp:Repeater>
<HeaderTemplate>
    <Table ...>
</HeaderTemplate>
<ItemTemplate>
  <tr Class="row">
    <td> ... </td>
    <td> ... </td>
  </tr>
</ItemTepmlate>
<FooterTemplate></Table></FooterTemplate>

However, when I followed the same structure for my DataList, it appeared messy and confusing. Here is how my DataList is set up:

<asp:DataList ID="dlStages" runat="server" DataKeyField="Priority" OnCancelCommand="dlStages_CancelCommand"
    OnEditCommand="dlStages_EditCommand" 
    OnUpdateCommand="dlStages_UpdateCommand" BorderWidth="2" 
     GridLines="Both" CellPadding="20" CellSpacing="20" Width="99%" 
    onitemdatabound="dlStages_ItemDataBound">

    <HeaderTemplate>

            <th>
                Priority
            </th>
            <th>
                Stage
            </th>
            <th>
                Description
            </th>
            <th>
                Command
            </th>
            <th>
                Expected End Date
            </th>
    </HeaderTemplate>
    <ItemTemplate>

            <td>
                <%# Eval("Priority") %>
            </td>
            <td>
                <%# Eval("StatusName") %>
            </td>
            <td>
                <%# Eval("Comments") %>
            </td>
            <td>
                <asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" CommandName="edit"></asp:LinkButton>
            </td>
            <td>
                <%# Eval("ExpectedEndDate", "{0:dd-MM-yyyy}")%>
            </td>

    </ItemTemplate>        
    <EditItemTemplate>

            <td>
                <%# Eval("Priority") %>
            </td>
            <td>
                <%# Eval("StatusName") %>
            </td>
            <td>
                <asp:TextBox ID="txtComments" runat="server" Text='<%# Eval("Comments") %>' Width="800px"
                    Height="48px" TextMode="MultiLine"></asp:TextBox>
            </td>
            <td>
                <asp:LinkButton ID="lbtnUpdate" runat="server" Text="Update" CommandName="update"></asp:LinkButton>
                /
                <asp:LinkButton ID="lbtnCancel" runat="server" Text="Cancel" CommandName="cancel"></asp:LinkButton>
            </td>
            <td>

                <asp:TextBox runat="server" ID="txtDate"></asp:TextBox>
                <asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="txtDate"
                    TargetControlID="txtDate" Format="dd-MM-yyyy">
                </asp:CalendarExtender>
            </td>
    </EditItemTemplate>
</asp:DataList>

Trying to apply the CSS class to each row in the DataList, I wrote the following code in the code behind:

protected void dlStages_ItemDataBound(object sender, DataListItemEventArgs e)
{
    e.Item.CssClass = "row";
}

However, this did not result in a Zebra table as expected. Instead, an unknown Grey color appeared in the first column. Can someone point out the issue with my code and suggest a solution to implement a Zebra table within the DataList control in my scenario?

Appreciate any assistance provided.

P.S. I acknowledge that my question may lack clarity, so feel free to ask for further clarification.

Answer №1

Check out this similar inquiry: Inserting image inside List

  1. Make sure to include the <tr> tags in your templates to avoid the DataList generating stray <td> tags.

  2. Utilize the RepeatLayout attribute with a value of Table to automatically add the <table> tags when using the DataList.

Answer №2

Although this question may be old, it's important to remember to properly set the ItemStyle and AlternatingItemStyle properties of the asp:DataList in order to achieve the desired striping effect. You can do this by specifying

ItemStyle="normalRow" AlternatingItemStyle="greenRow"

Make sure your datalist tag begins like this:

<style type="text/css>
.normalRow { background-color: white; }
.greenRow { background-color: green; }
</style>
<asp:DataList ID="dlStages" runat="server" DataKeyField="Priority" OnCancelCommand="dlStages_CancelCommand"
    OnEditCommand="dlStages_EditCommand" 
    OnUpdateCommand="dlStages_UpdateCommand" BorderWidth="2" 
     GridLines="Both" CellPadding="20" CellSpacing="20" Width="99%" 
    ItemStyle="normalRow" AlternatingItemStyle="greenRow"
    onitemdatabound="dlStages_ItemDataBound">

Answer №3

To utilize bootstrap classes within the DataList control's CssClass property, you can do the following:

CssClass="table table-striped"

Next, make adjustments to the table-striped styling:

.table-stripedCSS > tbody > tr:nth-child(odd) > td,
.table-stripedCSS > tbody > tr:nth-child(odd) > th {
    background-color:red;

    /*Customize the color as needed*/
}

For further details:
Bootstrap table striped: How do I change the stripe background colour?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the text exceeds its container, the CSS property overflow will display only the

.example{ overflow:auto; } <div class="example"> <p>Some Additional Text Here<P> </div> This particular code snippet showcases the initial portion of the text, allowing for scrolling down to reveal items 5 and 6: 1 2 ...

What is the best way to initiate a function upon each page load?

(Apologies in advance for any English mistakes) Hello everyone, I am currently developing a simple Chrome extension to edit certain graphics and text fields on a website (Freshdesk) that cannot be modified directly on the site due to proprietary code. ...

Guide on altering the background color of dynamically generated textboxes in R Shiny

textInput(paste0("inp1-", wid),label = NULL,value = record$Current_week) This code was used to dynamically generate text input boxes, where the id for each box is determined by a number called wid. I attempted to change the background color using the fol ...

What is the best way to implement an alert box in MVC without triggering a page redirection?

I have a specific requirement to display a custom alert box in order to confirm the user's intention to delete an item. Once the item is deleted, another alert box should appear confirming that the deletion was successful, prompting the user to click ...

Creating a Dynamic Slideshow on Autopilot

My JavaScript skills are not perfect and I'm feeling a bit lost. I have this code for a slideshow, but I want to make it automatic while also allowing users to navigate between images freely. I'm struggling to figure out how to implement this fun ...

What is the best way to send props to a CSS module in React?

Currently utilizing NextJS, I have a route named about that is displayed through page.js. Within /about/page.js, I am incorporating a component called <Hero /> and aiming to pass a prop to <Hero heroHeight={'height: 400px'} /> to my C ...

Issue with the first-child selector

Is this supposed to work or am I losing my mind? .project.work:first-child:before { content: 'Projects'; } .project.research:first-child:before { content: 'Research'; } <div class="project work"> <p>abcdef</p> ...

Display the user's location on Google Maps in addition to all other markers for a comprehensive view

How can I modify the code to prompt the user for their location only when they click on a specific button, and then show all markers on the map? This jsfiddle illustrates the current issues: Google map loads markers from a database, and the user is asked b ...

Issues with CSS causing items to not line up in a single row

I'm currently working on an html/css page and trying to align the items of the agenda class in the same row, centered on the page. https://i.sstatic.net/8TjG8.png Here's the code I've been using: <html lang="en" xmlns="ht ...

Having difficulty compiling Bootstrap css with grunt

After struggling for two days and finding numerous online tutorials, I still require assistance. My goal is to add a namespace to Bootstrap so that I can use it in Salesforce without conflicting with Salesforce's stylesheet. I plan on wrapping the boo ...

The sidebar remains fixed in height and does not expand vertically

Need Help: My sidebar won't expand in height when I update content. Is there a way to make it adjust using just HTML and CSS? html code: <div id="main"> <section> More text goes here... </section> <div id="sideb ...

The QWebview is not rendering the HTML page correctly

I am currently working on a software project that includes a QWebView for displaying HTML files. However, I am facing an issue with the welcome page not displaying correctly. Instead of having an image between the red lines at the center, it appears unusua ...

Steer your keyboard attention towards the parent element that embodies a list

My implementation focuses on making drop down menus accessible via keyboard input using HTML/CSS and JS/jQuery events. The goal of keyboard accessibility includes: Tab key to navigate the menu elements. Pressing the down arrow key opens a focused menu. ...

What can be done to prevent the angular material select from overflowing the screen?

I have integrated an Angular Material Select component into my application, which can be found here: https://material.angular.io/components/select/overview. The issue I am facing is that when the select element is positioned near the bottom of the screen a ...

Definition of ASP.NET Ajax 4 and the Library of Microsoft Ajax

I understand that the former is designed for Web Forms ASP.Net only, while the latter (currently preview 6) is suitable for both classic and MVC. Can you please provide a brief description/usage for each? When referring to ASP.NET Ajax 4, I am talking abo ...

What is the best approach to obtain an inventory of inputs from a particular viewpoint?

I am facing an issue where I have a list of products that require the quantities to be returned along with the reason for return. However, I cannot modify the referenced model as it has an impact on multiple values. Can someone guide me on how to retrieve ...

How to position and center a div layer over another div

I have been struggling to place a div over another div that contains an image. The div needs to have a simple HTML link just like the example shown in this picture: However, when the page loads, the URL is not centered on the image as you can see on the l ...

Step-by-step guide on applying a universal CSS class to every component within an application

I am in the process of building a multilingual react application and I am looking for a solution to dynamically apply certain CSS styles to most components based on the chosen language. I am seeking something akin to direction-rtl and direction-ltr, whic ...

What is the best way to retrieve a username and password from my database using C#?

In my btn_click event, I have the code snippet shown below: SqlConnection connection = new SqlConnection("server=.;database=bss;user id=ab;pwd=ab"); connection.Open(); SqlCommand command = new SqlCommand("select * from login where username='" + txt4 ...

Invoke JavaScript when the close button 'X' on the JQuery popup is clicked

I am implementing a Jquery pop up in my code: <script type="text/javascript"> function showAccessDialog() { var modal_dialog = $("#modal_dialog"); modal_dialog.dialog ( { title: "Access Lev ...