Tips SistemThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
Artikel ini menjelaskan cara menggunakan bersarang Repeater kontrol untuk menampilkan data secara hierarki. Anda dapat menerapkan konsep ini
untuk daftar-terikat kontrol lain.
Klik Visual C# proyek di bawah Jenis proyek, lalu klik ASP.Aplikasi NET Web di bawah Pola acu.
Dalam Lokasi kotak, menghapus WebApplication#, dan
kemudian ketik NestedRepeater. Jika Anda menggunakan server lokal,
meninggalkan nama server sebagai http://localhost. Lintasan berikut ini muncul di Lokasi kotak:
http://localhost/ NestedRepeater
Klik Oke.
Dalam Solusi Explorer, klik kanan NestedRepeater proyek nama node, arahkan ke Tambahkan, lalu klik Tambahkan formulir Web.
Untuk nama formulir Web, ketikNestedRepeater, klik Terbuka.
Formulir Web baru dibuat. Membuka dalam Design View di
Integrated Development Environment (IDE) Microsoft Visual Studio.NET. Dari
Toolbox, pilih Repeater kontrol, dan kemudian tarik ke halaman formulir Web.
Perubahan ID properti ini Repeater kontrol untuk parentRepeater.
Beralih ke tampilan HTML untuk formulir Web ini. Untuk melakukannya, klik
The HTML tab di sudut kiri bawah perancang. The Repeater kontrol menghasilkan kode HTML berikut:
Dalam solusi Explorer, klik kanan NestedRepeater.aspx, lalu klik Lihat kode untuk beralih ke NestedRepeater.aspx.cs kode-belakang
file.
Tambah deklarasi namespace berikut ke atas
berkas:
using System.Data;
using System.Data.SqlClient;
Tambahkan kode berikut untuk Page_Load acara untuk membuat sambungan untuk Pub database, dan kemudian untuk mengikat Penulis tabel untuk Repeater kontrol:
public void Page_Load(object sender, EventArgs e)
{
//Create the connection and DataAdapter for the Authors table.
SqlConnection cnn = new SqlConnection("server=(local);database=pubs; Integrated Security=SSPI");
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds,"authors");
//Insert code in step 4 of the next section here.
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["authors"];
Page.DataBind();
//Close the connection.
cnn.Close();
}
CATATAN: Anda mungkin harus mengubah string koneksi database sebagai
sesuai dengan lingkungan Anda.
Menyimpan semua file.
Dalam Solusi Explorer, klik kanan
NestedRepeater.aspx, dan kemudian klik Set sebagai halaman awal.
Pada Bangun Klik menu Membangun solusi untuk mengkompilasi proyek.
Tampilkan halaman .aspx di browser, dan kemudian memastikan bahwa
Halaman bekerja sejauh ini.
Setelah Anda mengatur DataSource properti untuk anak Repeater kontrol, kode HTML untuk dua Repeater kontrol (orangtua dan anak) muncul sebagai berikut:
Dalam kode di belakang halaman, mengganti baris berikut di Page_Load peristiwa
//Insert code in step 4 of the next section here.
dengan kode berikut:
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles");
//Create the relation between the Authors and Titles tables.
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]);
Ini menambah Judul tabel untuk DataSet, dan kemudian menambahkan hubungan antara penulis dan judul
tabel.
Simpan dan mengkompilasi aplikasi.
Melihat halaman dalam browser, dan kemudian memastikan bahwa halaman
bekerja begitu jauh. Output akan muncul sebagai berikut:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NestedRepeater
{
public class NestedRepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parentRepeater;
public NestedRepeater()
{
Page.Init += new System.EventHandler(Page_Init);
}
public void Page_Load(object sender, EventArgs e)
{
//Create the connection and DataAdapter for the Authors table.
SqlConnection cnn = new SqlConnection("server=(local);database=pubs; Integrated Security=SSPI ;");
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds,"authors");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["authors"];
Page.DataBind();
//Close the connection.
cnn.Close();
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:306154
(http://support.microsoft.com/kb/306154/en-us/
)
Mondja el, hogy miért, és tudassa velünk, miként tudnánk javítani az információ min?ségén
Köszönjük! Ez az információ segítséget nyújt számunkra, hogy fejlesszük a támogatási tartalmat. További támogatási lehet?ségekért keresse fel a következ? webhelyet: Súgó és támogatási webhely.