[英]Create Listview programmatically in asp.net
我是ASP.net的新手,我想以編程方式創建一個動態的Listview組件。 我找到了有關如何針對Gridview和Datatable而不是Listview進行操作的示例。 可能嗎? 有人知道好的教程嗎?
嘗試這個
private void CreateMyListView()
{
// Create a new ListView control.
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
//Creat columns:
ColumnHeader column1 = new ColumnHeader();
column1.Text = "Customer ID";
column1.Width = 159;
column1.TextAlign = HorizontalAlignment.Left;
ColumnHeader column2 = new ColumnHeader();
column2.Text = "Customer name";
column2.Width = 202;
column2.TextAlign = HorizontalAlignment.Left;
//Add columns to the ListView:
listView1.Columns.Add(column1);
listView1.Columns.Add(column2);
// Add the ListView to the control collection.
this.Controls.Add(listView1);
}
或看看那個例子
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class listview
Inherits Form
Friend WithEvents btnCreate As Button
Public Sub New()
Me.InitializeComponent()
End Sub
Private Sub InitializeComponent()
btnCreate = New Button
btnCreate.Text = "Create"
btnCreate.Location = New Point(10, 10)
Me.Controls.Add(btnCreate)
Text = "Countries Statistics"
Size = New Size(450, 245)
StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click
Dim lvwCountries As ListView = New ListView
lvwCountries.Location = New Point(10, 40)
lvwCountries.Width = 420
lvwCountries.Height = 160
Controls.Add(lvwCountries)
End Sub
Public Shared Sub Main()
Application.Run(New Exercise)
End Sub
End Class
有關如何完成此任務的基本思想。 關鍵概念與GridView
所需的概念相同。
1)您需要在頁面上的某個位置放置ListView
它的容器 。
2)此容器需要在服務器上運行 ,因此您的C#代碼(服務器評估)可以向其中添加ListView
。 您可以使用兩個示例容器: Panel
和帶有runat=server
屬性的標准div
標簽。
3)選擇何時 調用創建ListView的代碼,以及如何 調用 。 我建議您將其定義為一種方法 ,並從所需的任何事件中調用它,例如:
protected void Page_Load(object sender, EventArgs e)
{
// Call your method here so the ListView is created
CreateListView();
}
private void CreateListView()
{
// Code to create ListView here
}
4)使用上述方法中的以下代碼創建ListView
並將其添加到容器中,如下所示:
var myListView = new ListView();
containerName.Controls.Add(myListView);
您將需要添加到ListView
的屬性中,以使其在明顯的數據綁定之上具有美觀的美感。
在此頁面上找到的代碼具有您最可能想使用的一些示例屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.