获取Repeter的Item和ItemIndex/CommandArgument实现思路与代码
更新时间:2021年9月22日 10:17 点击:1472
首先看看效果:
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
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
相关文章
- 这篇文章主要介绍了C#实现让ListBox适应最大Item宽度的方法,涉及ListBox控件的操作技巧,需要的朋友可以参考下...2020-06-25
DevExpress实现根据行,列索引来获取RepositoryItem的方法
这篇文章主要介绍了DevExpress实现根据行,列索引来获取RepositoryItem的方法,需要的朋友可以参考下...2020-06-25Repeater控件动态变更列(Header,Item和Foot)信息实现思路
需求开发一个小报表,显示最近五个月的summary的数量统计,报表会随月份的变化而变化,接下来为大家详细介绍下实现方法,感兴趣的各位不要错过了哈...2021-09-22c# WPF中自定义加载时实现带动画效果的Form和FormItem
这篇文章主要介绍了c# WPF中自定义加载时实现带动画效果的Form和FormItem,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-05- 下面小编就为大家带来一篇C#获取ListView鼠标下的Item实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
用javascript为DropDownList控件下拉式选择添加一个Item至定义索引位置
用Javascript为DropDownList控件下拉式选择添加一个Item至定义索引位置;准备数据,创建一个对象,将是存储DropDownList控件每个Item数据...2021-09-22- 这篇文章主要给大家介绍了关于WPF自定义MenuItem样式的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用WPF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
- 这篇文章主要介绍了Python for循环与getitem的关系详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-02
获取Repeter的Item和ItemIndex/CommandArgument实现思路与代码
Repeater控件,放在ItemTemplate内的铵钮OnClick之后,获取Repeater的Item,ItemIndex,CommandArgument,CommandName以及绑定的字段值附演示动画感兴趣的朋友可以了解下...2021-09-22- 在本文中我们给大家分享了关于C#的ListBox中的Item拖拽的功能代码分享,对此有需要的朋友参考学习下。...2020-06-25
element-ui修改el-form-item样式的详细示例
在使用element-ui组件库的时候经常需要修改原有样式,下面这篇文章主要给大家介绍了关于element-ui修改el-form-item样式的详细示例,文中通过实例代码介绍的非常详细,需要的朋友可以参考下...2022-11-15- 这篇文章主要为大家介绍了字节封装React组件手机号自动校验格式FormItem,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...2022-08-16