Menampilkan data ke Table ASP.net

Web Kategori.ASPX 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Kategori.aspx.cs" Inherits="Kategori"
    MasterPageFile="~/MasterPage.master" %>

<asp:Content ContentPlaceHolderID="Body" runat="server">
    Kategori pake GridView <br/>
    <asp:HyperLink NavigateUrl="~/Kategori/AddKategori.aspx" runat="server">Tambah Kategori</asp:HyperLink>
    <asp:GridView ID="DaftarKategori" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"
         >
        <AlternatingRowStyle BackColor="White" />

        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />

         <Columns>
            <asp:BoundField DataField="namaKategori" HeaderText="Nama Kategori" />
            <asp:BoundField DataField="margin" HeaderText="Margin" />
             <asp:BoundField DataField="ur" HeaderText="Uang resep" />

            <asp:HyperLinkField DataNavigateUrlFields="namaKategori" DataNavigateUrlFormatString="EditKategori.aspx?namaKategori={0}" HeaderText="Edit" Text="Edit" />
            <asp:HyperLinkField DataNavigateUrlFields="namaKategori" DataNavigateUrlFormatString="HapusKategori.aspx?namaKategori={0}" HeaderText="Hapus" Text="Hapus" />

        </Columns>
    </asp:GridView>

    <br/>Kategori pake ListView <br/>
    <table class="table table-striped">
        <tr><td>Nama Kategori</td><td>Margin</td><td>Uang Resep</td><td>Edit</td><td>Hapus</td></tr>
        <asp:ListView ID="DaftarKategoriListView" runat="server" OnItemCommand="DaftarKategoriListView_Event">
            <ItemTemplate>
                <tr><td><%# Eval("namaKategori") %></td><td><%# Eval("margin") %></td><td><%# Eval("ur") %></td>
                    <td>
                        <asp:LinkButton  runat="server" Text="Edit" Visible="true" OnClientClick="return true" CommandName="EditKategori"
                           CommandArgument='<%# Eval("namaKategori") %>' CssClass="btn btn-mini">
                            <i class="icon-pencil"></i>Edit
                        </asp:LinkButton>
                    </td>
                    <td>
                        <asp:Button ID="Button1" runat="server" CausesValidation="false"
                        OnClientClick="return confirm('Are you sure you want to delete?');"
                        Text="Hapus" CommandName="HapusKategori" CommandArgument='<%# Eval("namaKategori") %>' />

                        <a class='btn btn-mini btn-danger lnk-delete' data-toggle='modal' href='#delete-confirm' data-data='Sebuah Data'
                        data-url='<%# ResolveUrl("~/Kategori/HapusKategori.aspx?namaKategori="+Eval("namaKategori")) %>'>
                        <i class='icon-remove icon-white'></i>Delete</a>
                    </td>
                    </tr>
            </ItemTemplate>
        </asp:ListView>
    </table>

</asp:Content>

Backend (Kategori.aspx.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apotek.Business;

public partial class Kategori : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            DaftarKategori.DataSource = KategoriHelper.GetAllKategori();
            DaftarKategori.DataBind();

            DaftarKategoriListView.DataSource = KategoriHelper.GetAllKategori();
            DaftarKategoriListView.DataBind();

        }

    }
}

Business (KategoriHelper.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Apotek.Entities;

namespace Apotek.Business
{
    public static class KategoriHelper
    {
        public static List<kategori> GetAllKategori()
        {
            ApotekDataContext db = new ApotekDataContext();

            var query = from x in db.kategoris
                        select x;

            return query.ToList();
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: