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

所属分类: 网络编程 / ASP.NET 阅读数: 764
收藏 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
更多精彩内容其他人还在看

asp.net c# 抓取页面信息方法介绍

我们知道,一般网页中的信息是不断翻新的,这也要求我们定期的去抓这些新信息,但是这个“定期”该怎么理解,也就是多长时间需要 抓一次该页面,其实这个定期也就是页面缓存时间,在页面的缓存时间内我们再次抓取该网页是没有必要的,反而给人家服务器造成压力
收藏 0 赞 0 分享

ASP.NET 后台登录小技巧介绍

对于后台一些需要登录才能访问的页面,你是不是每次都去判断一下登录时保存在session或者cookie里面的值是否存在啊!
收藏 0 赞 0 分享

ASP.NET MVC Web API HttpClient简介

依稀还记得那个时候用WebClient,HttpWebRequest来发送一个请求,现在ASP.NET MVC4中自带了一个类HttpClient;需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net mvc路由篇 如何找到 IHttpHandler方法介绍

学习是使用asp.net已经有很长一段时间了,现在就来分析一下mvc的整过过程吧。个人计划写一个mvc系列的博文,仅从源代码的角度来分析mvc。在接触mvc时我们一定会经历路由,那么路由这东东是怎么搞出来的啊
收藏 0 赞 0 分享

C#与.net高级编程 C#的多态介绍

封装、继承、多态,面向对象的三大特性,前两项理解相对容易,但要理解多态,特别是深入的了解,对于初学者而言可能就会有一定困难了
收藏 0 赞 0 分享

使用Asp.net Mvc3 Razor视图方式扩展JQuery UI Widgets方法介绍

jquery easyui grid或者extjs grid,jtable的代码非常简洁、对于grid功能要求不是很复杂的情况下,强烈推荐大家使用
收藏 0 赞 0 分享

Oracle中TO_DATE格式介绍

Oracle中TO_DATE格式介绍;可供需求的朋友参考
收藏 0 赞 0 分享

DataGridView控件详细介绍

DataGridView是用于Windows Froms 2.0的新网格控件。它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特性
收藏 0 赞 0 分享

jdk环境变量配置

jdk环境变量配置,可供参考
收藏 0 赞 0 分享

如何取消.net后台线程的执行

在使用多线程模型进行编程时,经常遇到的问题之一是,当我们关闭前台的UI线程时,后台的辅助线程仍然处于活动状态,从而导致整个应用程序无法正常退出
收藏 0 赞 0 分享
查看更多