Rabu, 19 Oktober 2011

PEMROGRAMAN WEB (pert - 6)

RESUME PEMROGRAMAN WEB
PERTEMUAN 6

Dosen : Kurniawan Jatmika. S. Kom


GridView

GridView kontrol adalah penerus datagrid ASP.NET 1.x kontrol. Ini memberikan lebih banyak fleksibilitas dalam menampilkan dan bekerja dengan data dari database Anda dibandingkan dengan kontrol lain. Kontrol GridView memungkinkan Anda untuk terhubung ke sumber data dan menampilkan data adalah format tabel, namun Anda memiliki banyak pilihan untuk menyesuaikan tampilan dan nuansa. Ketika diberikan pada halaman, pada umumnya dilaksanakan melalui tag HTML <table>.

Sifat-sifatnya seperti BackColor, ForeColor, bordercolor, BorderStyle, BorderWidth, Tinggi dll diimplementasikan melalui properites gaya tag <tahle>.
Berikut adalah beberapa sifat penting yang sangat berguna.

Behavior Properties of the GridView Control
AllowPaging true/false. Indicate whether the control should support paging.
AllowSorting true/false. Indicate whether the control should support sorting.
SortExpression Gets the current sort expression (field name) that determines the order of the row.
SortDirection Gets the sorting direction of the column sorted currently (Ascending/Descending).
DataSource Gets or sets the data source object that contains the data to populate the control.
DataSourceID Indicate the bound data source control to use (Generally used when we are using SqlDataSource or AccessDataSource to bind the data, See 1st Grid example).
AutoGenerateEditButton true/false. Indicates whether a separate column should be added to edit the record.
AutoGenerateDeleteButton true/false. Indicates whether a separate column should be added to delete the record.
AutoGenerateSelectButton true/false. Indicate whether a separate column should be added to selecat a particular record.
AutoGenerateColumns true/false. Indicate whether columns are automatically created for each field of the data source. The default is true.
Style Properties of the GridView Control
AlternatingRowStyle Defines the style properties for every alternate row in the GridView.
EditRowStyle Defines the style properties for the row in EditView (When you click Edit button for a row, the row will appear in this style).
RowStyle Defines the style properties of the rows of the GridView.
PagerStyle Defines the style properties of Pager of the GridView. (If AllowPaging=true, the page number row appears in this style)
EmptyDataRowStyle Defines the style properties of the empty row, which appears if there is no records in the data source.
HeaderStyle Defines the style properties of the header of the GridView. (The column header appears in this style.)
FooterStyle Defines the style properties of the footer of GridView.
Appearance Properties of the GridView Control
CellPadding Indicates the space in pixel between the cells and the border of the GridView.
CellSpacing Indicates the space in pixel between cells.
GridLines Both/Horizontal/Vertical/None. Indicates whether GrdiLines should appear or not, if yes Horizontal, Vertical or Both.
HorizontalAlign Indicates the horizontal align of the GridView.
EmptyDataText Indicates the text to appear when there is no record in the data source.
ShowFooter Indicates whether the footer should appear or not.
ShowHeader Indicates whether the header should appear or not. (The column name of the GridView)
BackImageUrl Indicates the location of the image that should display as a background of the GridView.
Caption Gets or sets the caption of the GridView.
CaptionAlign left/center/right. Gets or sets the horizontal position of the GridView caption.
State Properties of GridView Control
Columns Gets the collection of objects that represent the columns in the GridView.
EditIndex Gets or sets the 0-based index that identifies the row currently to be edited.
FooterRow Returns a GridViewRow object that represents the footer of the GridView.
HeaderRow Returns a GridViewRow object that represents the header of the GridView.
PageCount Gets the number of the pages required to display the reocrds of the data source.
PageIndex Gets or sets the 0-based page index.
PageIndex Gets or sets the number of records to display in one page of GridView.
Rows Gets a collection of GridViewRow objects that represents the currently displayed rows in the GridView.
DataKeyNames Gets an array that contains the names of the primary key field of the currently displayed rows in the GridView.
DataKeys Gets a collection of DataKey objects that represent the value of the primary key fields set in DataKeyNames property of the GridView.
Events associated with GridView Control
PageIndexChanging, PageIndexChanged Both events occur when the page link is clicked. They fire before and after GridView handles the paging operation respectively.
RowCancelingEdit Fires when Cancel button is clicked in Edit mode of GridView.
RowCommand Fires when a button is clicked on any row of GridView.
RowCreated Fires when a new row is created in GridView.
RowDataBound Fires when row is bound to the data in GridView.
RowDeleting,RowDeleted Both events fires when Delete button of a row is clicked. They fire before and after GridView handles deleting operaton of the row respectively.
RowEditing Fires when a Edit button of a row is clicked but before the GridView hanldes the Edit operation.
RowUpdating, RowUpdated Both events fire when a update button of a row is clicked. They fire before and after GridView control update operation respectively.
Sorting, Sorted Both events fire when column header link is clicked. They fire before and after the GridView handler the Sort operation respectively.
DEMO : Button Show Source Code
1st Grid: Simple GridView with SqlDataSource
Delete?AutoIDNameAddressPhoneCity
EditDelete 6921
EditDelete 69223226594444erewre
EditDelete 6919aasadaadadada
EditDelete 6797AAAAAggjvuig;
EditDelete 6907ABCDEF984612389n kdkqwjdj jawjdfjfrj
EditDelete 6920asaadaadsadasda
EditDelete 6916dfaffadfadfafadsf
EditDelete 6897dfsdfsdfsdfsdfdsffsdfsd
12345
Note: Updating this GridView will not affect 2nd GridView listing unless you come to this page without page postback as that GridView data has been bound in not IsPostBack condition.

2nd Grid: GridView with CodeBehind Data Bound
AutoIDNameAddressPhoneCity
6921
69223226594444erewre
6919aasadaadadada
6797AAAAAggjvuig;
6907ABCDEF984612389n kdkqwjdj jawjdfjfrj
6920asaadaadsadasda
6916dfaffadfadfafadsf
6897dfsdfsdfsdfsdfdsffsdfsd
6898dfsdfsdfsdfsdfdsffsdfsd
6899dfsdfsdfsdfsdfdsffsdfsd
6900dfsdfsdfsdfsdfdsffsdfsd
6901dfsdfsdfsdfsdfdsffsdfsd
6894dfsdfsdfsdfsdfdsffsdfsd
6895dfsdfsdfsdfsdfdsffsdfsd
6896dfsdfsdfsdfsdfdsffsdfsd
6904dfsdfsdfsdfsdfdsffsdfsd
6905dfsdfsdfsdfsdfdsffsdfsd
6903dfsdfhsdfsdfsdfdsffsdfsd
6917fdfafasfafadfafdaf
6893fdgfdgfdfgdfdfgdf
6914fgdgdfgdfgfdgfg
6915fsdgsdfsdfgsdf56346dfghdfgh
6909madhuhyd999999asdsasa
6918sasasasa
6860sasaasasa343sdd
6887sdsdasdsasadsadasdsdasdassadassdsadasd
6811sssvvvsssssga
6854sssssssdsdsdsdsdsdsdsdsd
6771sstyhtudddffee
6815testthis 3214123berlin
6809tetwera1231231233rte
6861thjghjvgttt
6759uiiuiu wciuiuii
6824varunrace course9703493247Coimbatore
6889vvxcvxcvxcvxcvxcvxcvcx
6876wewewewewewewewewwewew
6913xcxxcxxcxcxc
6891xggxg46456xgxd
6912yogichennai9952214665chennai

// 1st GridView /////////////////////////
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Caption="1st Grid: Simple GridView with SqlDataSource"
     AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" 
      DataKeyNames="AutoID" PageSize="8">
      <Columns>
        <asp:TemplateField HeaderText="Delete?">
            <ItemTemplate>
                <asp:LinkButton ID="lnk1" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?')" CommandName="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'
     SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]"
     DeleteCommand="Delete FROM SampleForTutorials WHERE AutoID = @AutoID"
     UpdateCommand="UPDATE SampleForTutorials SET Name = @Name, Address = @Address, Phone = @Phone, City = @City WHERE AutoID = @AutoID">
        <DeleteParameters>
            <asp:Parameter Name="AutoID" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="AutoID" Type="Int32" />
            <asp:Parameter Name="Name" Type="string" Size="50" />
            <asp:Parameter Name="Address" Type="string" Size="200" />
            <asp:Parameter Name="Phone" Type="int32" />
            <asp:Parameter Name="City" Type="string" Size="20" />
        </UpdateParameters>
     </asp:SqlDataSource>
     
     
     // 2nd GridView /////////////////////////
<asp:GridView ID="GridView2" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
 Caption="2nd Grid: GridView with CodeBehind Data Bound" AllowPaging="False" AllowSorting="False"
    BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
    <FooterStyle BackColor="Tan" />
    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" Font-Bold="True" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>     
 
 
 // CODE BEHIND
 
// Bind the GridView /////////////////////////
private void BindGridView()
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
    SqlConnection conn = new SqlConnection(connStr);
    conn.Open();
    SqlDataAdapter dAd = null;
    DataSet dSet = new DataSet();
    try
    {
        string strSql = "Select * FROM SampleForTutorials ORDER BY [Name]";
        dAd = new SqlDataAdapter(strSql, conn);
        dAd.Fill(dSet, "SampleTable");

        GridView2.DataSource = dSet.Tables["SampleTable"].DefaultView;
        GridView2.DataBind();
        
    }
    catch (Exception ee)
    {
        lblError.Text = "Error occured. <br>+" + ee.Message.ToString();
    }
    finally
    {
        dSet.Dispose();
        dAd.Dispose();
        conn.Close();
        conn.Dispose();
    }
} 
 
 
Sumber : http://www.dotnetfunda.com/tutorials/controls/gridview.aspx 

PEMROGRAMAN WEB (pert - 5)

RESUME PEMROGRAMAN WEB
PERTEMUAN 5

Dosen : Kurniawan Jatmika. S. Kom

FORMVIEW DAN QUERY 

Formview

Kontrol FormView mirip dengan DetailsView. Daripada menggunakan tag bidang seperti <asp:BoundField>, FormView yang menentukan data menggunakan tag template seperti <ItemTemplate>.

Biasanya (yaitu, ketika adaptor tidak digunakan) FormView merender <table> untuk lay out header, footer, paging dan item data daerah.

Adaptor dapat digunakan untuk menggantikan <table> ini dengan beberapa <div> tag bersarang yang penampilannya dan posisi yang mudah ditata dengan CSS.

Contoh

 

HTML Snippets

Snippet of HTML when using the adapters

ASP.NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<div class="PrettyFormView" id="ctl00_ctl00_MainContent_LiveExample_FormView1">
  <div class="AspNet-FormView">
      <div class="AspNet-FormView-Header">
        Author Details
      </div>
      <div class="AspNet-FormView-Data">
        
                <div class="Sample-Contact">
                    <div class="Sample-Name">
                        Abraham Bennet
                    </div>
                    <div class="Sample-Address">
                        6223 Bateman St.<br />
                        Berkeley, CA<br />
                        94705
                    </div>
                    <div class="Sample-Phone">
                        415 658-9932
                    </div>
                </div>
            
      </div>
      <div class="AspNet-FormView-Pagination">
        <span class="AspNet-FormView-ActivePage">1</span><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$2')">2</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$3')">3</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$4')">4</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$5')">5</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$6')">6</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$7')">7</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$8')">8</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$9')">9</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$10')">10</a><a class="AspNet-FormView-OtherPage" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LiveExample$FormView1','Page$11')">...</a>
      </div>

  </div>
</div>

Snippet of HTML when not using the adapters

ASP.NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<table cellspacing="2" CssSelectorClass="PrettyFormView" border="0" id="ctl00_ctl00_MainContent_LiveExample_FormView1" style="background-color:White;border-style:None;">
  <tr style="color:#F7F6F3;background-color:#5D7B9D;font-weight:bold;">
    <td colspan="2">Author Details</td>
  </tr><tr style="color:#333333;background-color:White;">
    <td colspan="2">
                <div class="Sample-Contact">
                    <div class="Sample-Name">
                        Abraham Bennet
                    </div>
                    <div class="Sample-Address">
                        6223 Bateman St.<br />
                        Berkeley, CA<br />
                        94705
                    </div>
                    <div class="Sample-Phone">
                        415 658-9932
                    </div>
                </div>
            </td>
  </tr><tr align="center" style="color:Cyan;background-color:#284775;">
    <td colspan="2"><table border="0">
      <tr>
        <td><span>1</span></td><td><a href="javascript:__doPostBack('ctl00$ctl
     </tr>
    </table></td>
  </tr>
</table> 


Sumber : http://www.asp.net/cssadapters/FormView.aspx
 
 

Query ASP.net

Pernyataan SQL

Sebagian besar tindakan yang Anda perlu melakukan pada database dilakukan dengan pernyataan SQL.
Pernyataan SQL berikut akan memilih semua catatan dalam tabel "Peserta":
SELECT * FROM Persons
Dalam tutorial ini kita akan mengajarkan anda semua tentang pernyataan SQL yang berbeda.

Perlu Pikiran itu ...

  • SQL tidak bersifat case sensitive

Titik koma setelah SQL Laporan?

Beberapa sistem database membutuhkan tanda titik koma di akhir setiap pernyataan SQL.
Titik koma adalah cara standar untuk memisahkan setiap pernyataan SQL dalam sistem database yang memungkinkan lebih dari satu pernyataan SQL akan dieksekusi dalam panggilan yang sama ke server.
Kami menggunakan MS Access dan SQL Server 2000 dan kita tidak harus meletakkan titik koma setelah setiap pernyataan SQL, tetapi beberapa program database memaksa Anda untuk menggunakannya.

SQL DML dan DDL

SQL dapat dibagi menjadi dua bagian: Manipulasi data Language (DML) dan data Definition Language (DDL).
Permintaan dan perintah memperbarui membentuk bagian dari SQL DML:
  • SELECT - data ekstrak dari database
  • UPDATE - update data dalam database
  • DELETE - menghapus data yang dari database
  • INSERT INTO - menyisipkan data yang baru ke dalam database
Bagian DDL tabel database SQL izin yang akan dibuat atau dihapus. Hal ini juga mendefinisikan indeks (kunci), menentukan hubungan antara tabel, dan memberlakukan kendala antara tabel. Pernyataan DDL dalam SQL yang paling penting adalah:
  • CREATE DATABASE - membuat database baru
  • ALTER DATABASE - memodifikasi database
  • CREATE TABLE - membuat tabel baru
  • ALTER TABLE - memodifikasi tabel
  • DROP TABLE - menghapus tabel
  • CREATE INDEX - membuat index (kunci pencarian)
  • DROP INDEX - menghapus indeks

    Sumber : http://www.w3schools.com/sql/sql_syntax.asp

PEMROGRAMAN WEB (pert - 4)

RESUME PEMROGRAMAN WEB
PERTEMUAN 4

Dosen : Kurniawan Jatmika. S. Kom

REPEATER DAN DATALIST 

Repeater 
 
Kontrol Repeater digunakan untuk menampilkan daftar ulang item yang terikat untuk kontrol.

Mengikat sebuah DataSet ke Kontrol Repeater

Kontrol Repeater digunakan untuk menampilkan daftar ulang item yang terikat untuk kontrol. Kontrol Repeater dapat terikat pada tabel database, file XML, atau daftar item lain. Di sini kita akan menunjukkan bagaimana untuk mengikat sebuah file XML ke Repeater kontrol.
Kami akan menggunakan file XML berikut contoh kami di ("cdcatalog.xml"):
<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
<cd>
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <country>USA</country>
  <company>Columbia</company>
  <price>10.90</price>
  <year>1985</year>
</cd>
<cd>
  <title>Hide your heart</title>
  <artist>Bonnie Tyler</artist>
  <country>UK</country>
  <company>CBS Records</company>
  <price>9.90</price>
  <year>1988</year>
</cd>
<cd>
  <title>Greatest Hits</title>
  <artist>Dolly Parton</artist>
  <country>USA</country>
  <company>RCA</company>
  <price>9.90</price>
  <year>1982</year>
</cd>
<cd>
  <title>Still got the blues</title>
  <artist>Gary Moore</artist>
  <country>UK</country>
  <company>Virgin records</company>
  <price>10.20</price>
  <year>1990</year>
</cd>
<cd>
  <title>Eros</title>
  <artist>Eros Ramazzotti</artist>
  <country>EU</country>
  <company>BMG</company>
  <price>9.90</price>
  <year>1997</year>
</cd>
</catalog>
Lihatlah file XML: cdcatalog.xml
Pertama, impor "System.Data" namespace. Kita perlu namespace ini untuk bekerja dengan objek DataSet. Sertakan direktif berikut pada bagian atas sebuah halaman aspx.:
<%@ Import Namespace="System.Data" %>
Selanjutnya, ciptakan sebuah DataSet untuk file XML dan load file XML ke dalam DataSet saat halaman pertama dimuat:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub
Kemudian kita membuat kontrol Repeater dalam sebuah halaman. Aspx. Isi dari elemen <HeaderTemplate> tersebut diberikan pertama dan hanya sekali dalam output, maka isi dari elemen <ItemTemplate> diulang untuk setiap "catatan" dalam DataSet, dan terakhir, isi dari elemen <FooterTemplate> yang diberikan sekali dalam output:
<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>
Lalu kita tambahkan script yang menciptakan mengikat DataSet dan DataSet mycdcatalog ke kontrol Repeater. Kami juga mengisi kontrol Repeater dengan tag HTML dan mengikat data item ke sel-sel di bagian <ItemTemplate> dengan <% # Container.DataItem ("fieldName")%> metode:

Contoh

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

Tampilkan contoh »

Menggunakan <AlternatingItemTemplate>

Anda dapat menambahkan elemen <AlternatingItemTemplate> setelah elemen <ItemTemplate> untuk menggambarkan penampilan bolak-baris output. Pada contoh berikut setiap baris lain dalam tabel akan ditampilkan dalam warna abu-abu muda:

Contoh

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</AlternatingItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

Tampilkan contoh »

Menggunakan <SeparatorTemplate>

Unsur <SeparatorTemplate> dapat digunakan untuk menggambarkan pemisah antara setiap record. Contoh berikut menyisipkan sebuah garis horisontal antara setiap baris tabel:

Contoh

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>

<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

Sumber : http://www.w3schools.com/aspnet/aspnet_repeater.asp


Datalist

Kontrol DataList adalah, seperti kontrol Repeater, digunakan untuk menampilkan daftar ulang item yang terikat untuk kontrol. Namun, kontrol DataList menambah tabel di item data secara default.

Mengikat sebuah DataSet ke Kontrol DataList

Kontrol DataList adalah, seperti kontrol Repeater, digunakan untuk menampilkan daftar ulang item yang terikat untuk kontrol. Namun, kontrol DataList menambah tabel di item data secara default. Kontrol DataList dapat terikat pada tabel database, file XML, atau daftar item lain. Di sini kita akan menunjukkan bagaimana untuk mengikat sebuah file XML ke kontrol DataList.
Kami akan menggunakan file XML berikut contoh kami di ("cdcatalog.xml"):
<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
<cd>
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <country>USA</country>
  <company>Columbia</company>
  <price>10.90</price>
  <year>1985</year>
</cd>
<cd>
  <title>Hide your heart</title>
  <artist>Bonnie Tyler</artist>
  <country>UK</country>
  <company>CBS Records</company>
  <price>9.90</price>
  <year>1988</year>
</cd>
<cd>
  <title>Greatest Hits</title>
  <artist>Dolly Parton</artist>
  <country>USA</country>
  <company>RCA</company>
  <price>9.90</price>
  <year>1982</year>
</cd>
<cd>
  <title>Still got the blues</title>
  <artist>Gary Moore</artist>
  <country>UK</country>
  <company>Virgin records</company>
  <price>10.20</price>
  <year>1990</year>
</cd>
<cd>
  <title>Eros</title>
  <artist>Eros Ramazzotti</artist>
  <country>EU</country>
  <company>BMG</company>
  <price>9.90</price>
  <year>1997</year>
</cd>
</catalog>
Lihatlah file XML: cdcatalog.xml
Pertama, impor "System.Data" namespace. Kita perlu namespace ini untuk bekerja dengan objek DataSet. Sertakan direktif berikut pada bagian atas sebuah halaman aspx.:
<%@ Import Namespace="System.Data" %>
Selanjutnya, ciptakan sebuah DataSet untuk file XML dan load file XML ke dalam DataSet saat halaman pertama dimuat:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub
Kemudian kita membuat DataList dalam sebuah halaman. Aspx. Isi dari elemen <HeaderTemplate> tersebut diberikan pertama dan hanya sekali dalam output, maka isi dari elemen <ItemTemplate> diulang untuk setiap "catatan" dalam DataSet, dan terakhir, isi dari elemen <FooterTemplate> yang diberikan sekali dalam output:
<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog" runat="server">

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>
Lalu kita tambahkan script yang menciptakan mengikat DataSet dan DataSet mycdcatalog ke kontrol DataList. Kami juga mengisi kontrol DataList dengan <HeaderTemplate> yang berisi header dari tabel, sebuah <ItemTemplate> yang berisi item data untuk menampilkan, dan <FooterTemplate> yang berisi teks. Perhatikan bahwa atribut gridlines dari DataList diatur ke "baik" untuk menampilkan perbatasan tabel:

Contoh

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog"
gridlines="both" runat="server">

<HeaderTemplate>
My CD Catalog
</HeaderTemplate>

<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>

<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

Tampilkan contoh »

Menggunakan Styles

Anda juga dapat menambahkan gaya ke kontrol DataList untuk membuat output lebih mewah:

Contoh

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="true"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="true">

<HeaderTemplate>
My CD Catalog
</HeaderTemplate>

<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>

<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

Tampilkan contoh »

Menggunakan <AlternatingItemTemplate>

Anda dapat menambahkan elemen <AlternatingItemTemplate> setelah elemen <ItemTemplate> untuk menggambarkan penampilan bolak-baris output. Anda mungkin gaya data dalam bagian <AlternatingItemTemplate> dalam kontrol DataList:

Contoh

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="True"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
alternatingitemstyle-backcolor="#e8e8e8"
alternatingitemstyle-forecolor="#000000"
footerstyle-font-size="9pt"
footerstyle-font-italic="True">

<HeaderTemplate>
My CD Catalog
</HeaderTemplate>

<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>

<AlternatingItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</AlternatingItemTemplate>

<FooterTemplate>
&copy; Hege Refsnes
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>


Sumber : http://www.w3schools.com/aspnet/aspnet_datalist.asp
Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Laundry Detergent Coupons