获取Repeter的Item和ItemIndex/CommandArgument实现思路与代码

所属分类: 网络编程 / ASP.NET 阅读数: 738
收藏 0 赞 0 分享
首先看看效果

Repeater控件,放在ItemTemplate内的铵钮OnClick之后,获取Repeater的Item,ItemIndex,CommandArgument,CommandName以及绑定的字段值。
准备数据
复制代码 代码如下:

View Code
Imports Microsoft.VisualBasic
Namespace Insus.NET
Public Class Catalog
Private _ID As Integer
Private _Name As String
Public Property ID As Integer
Get
Return _ID
End Get
Set(value As Integer)
_ID = value
End Set
End Property
Public Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
End Class
End Namespace

复制代码 代码如下:

View Code
Private Function GetData() As List(Of Catalog)
Dim cls As New List(Of Catalog)
Dim cl As Catalog = New Catalog()
cl.ID = 1
cl.Name = "汽车"
cls.Add(cl)
cl = New Catalog()
cl.ID = 2
cl.Name = "时尚"
cls.Add(cl)
cl = New Catalog()
cl.ID = 3
cl.Name = "科技"
cls.Add(cl)
cl = New Catalog()
cl.ID = 5
cl.Name = "文化"
cls.Add(cl)
cl = New Catalog()
cl.ID = 6
cl.Name = "公益"
cls.Add(cl)
Return cls
End Function

在.aspx放置Repeater控件:
复制代码 代码如下:

View Code
<asp:Repeater ID="RepeaterCatalog" runat="server">
<HeaderTemplate>
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<td>ID
</td>
<td>Name
</td>
<td>Choose</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="LabelID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
</td>
<td>
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Select" OnClick="Button1_Click" CommandArgument='<%# Eval("ID")%>' CommandName="Choose" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

在.aspx.vb为Repeater控件绑定数据
复制代码 代码如下:

View Code
Imports Insus.NET
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Data_Binding()
End If
End Sub
Private Sub Data_Binding()
Me.RepeaterCatalog.DataSource = GetData()
Me.RepeaterCatalog.DataBind()
End Sub
End Class

接下来,我们写onclick事件,在写事件之前,先在.aspx放一个Label来显示事件结果:
复制代码 代码如下:

Process infor:
<asp:Label ID="LabelInfo" runat="server" Text=""></asp:Label>

复制代码 代码如下:

View Code
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim commandArgument As String = btn.CommandArgument
Dim commandName As String = btn.CommandName
Dim item As RepeaterItem = DirectCast(btn.NamingContainer, RepeaterItem)
Dim index As Integer = item.ItemIndex
Dim id As String = DirectCast(item.FindControl("LabelID"), Label).Text
Dim name As String = DirectCast(item.FindControl("LabelName"), Label).Text
Me.LabelInfo.Text = String.Format("Item index: {0}; CommandArgument: {1}; CommandName: {2}; ID: {3}; Name: {4};", index, commandArgument, commandName, id, name)
End Sub
更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多