[英]Create Listview programmatically in asp.net
I am new to ASP.net and I would like to programmatically create a dynamic Listview component. 我是ASP.net的新手,我想以编程方式创建一个动态的Listview组件。 I found examples on how to do that for Gridview and Datatable but not Listview.
我找到了有关如何针对Gridview和Datatable而不是Listview进行操作的示例。 Is it possible?
可能吗? Does anyone know of a good tutorial?
有人知道好的教程吗?
Try this 尝试这个
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);
}
Or take a look at that Example 或看看那个例子
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
Basic idea of how to approach this task. 有关如何完成此任务的基本思想。 The key concepts are the same ones a
GridView
would require. 关键概念与
GridView
所需的概念相同。
1) You need somewhere on the page to put the ListView
- a container for it. 1)您需要在页面上的某个位置放置
ListView
它的容器 。
2) This container needs to run at the server , so your C# code (which the server evaluates) can add the ListView
to it. 2)此容器需要在服务器上运行 ,因此您的C#代码(服务器评估)可以向其中添加
ListView
。 Two example containers you could use: a Panel
and a standard div
tag with the runat=server
property. 您可以使用两个示例容器:
Panel
和带有runat=server
属性的标准div
标签。
3) Choose when the code to create the ListView will be called and how . 3)选择何时 调用创建ListView的代码,以及如何 调用 。 I recommend you define it as a method and call it from whatever event you want eg:
我建议您将其定义为一种方法 ,并从所需的任何事件中调用它,例如:
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) Use below code in above method to create the ListView
and add it to the container like so: 4)使用上述方法中的以下代码创建
ListView
并将其添加到容器中,如下所示:
var myListView = new ListView();
containerName.Controls.Add(myListView);
You will need to add to the ListView
's properties for it to be aesthetically pleasing, on top of the obvious databinding. 您将需要添加到
ListView
的属性中,以使其在明显的数据绑定之上具有美观的美感。
The code found on this page has some example properties you will most likely want to use. 在此页面上找到的代码具有您最可能想使用的一些示例属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.