獲取Repeter的Item和ItemIndex/CommandArgument實現(xiàn)思路與代碼
更新時間:2013年01月17日 15:27:21 作者:
Repeater控件,放在ItemTemplate內(nèi)的銨鈕OnClick之后,獲取Repeater的Item,ItemIndex,CommandArgument,CommandName以及綁定的字段值附演示動畫感興趣的朋友可以了解下
首先看看效果:

Repeater控件,放在ItemTemplate內(nèi)的銨鈕OnClick之后,獲取Repeater的Item,ItemIndex,CommandArgument,CommandName以及綁定的字段值。
準(zhǔn)備數(shù)據(jù):
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控件綁定數(shù)據(jù):
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來顯示事件結(jié)果:
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內(nèi)的銨鈕OnClick之后,獲取Repeater的Item,ItemIndex,CommandArgument,CommandName以及綁定的字段值。
準(zhǔn)備數(shù)據(jù):
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
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控件:
復(fù)制代碼 代碼如下:
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控件綁定數(shù)據(jù):
復(fù)制代碼 代碼如下:
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來顯示事件結(jié)果:
復(fù)制代碼 代碼如下:
Process infor:
<asp:Label ID="LabelInfo" runat="server" Text=""></asp:Label>
復(fù)制代碼 代碼如下:
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
您可能感興趣的文章:
- 用Command對象和RecordSet對象向數(shù)據(jù)庫增加記錄哪一個更好
- 使用Jmail及Winwebmail發(fā)信時系統(tǒng)記錄中的錯誤:502 Error: command ...
- Delphi Command模式
- asp中command的在單條記錄時,有些字段顯示為空的問題
- javascript document.execCommand() 常用解析
- asp.net gridview的Rowcommand命令中獲取行索引的方法總結(jié)
- php設(shè)計模式 Command(命令模式)
- php設(shè)計模式 Command(命令模式)
- 解決VS2012 Express的There was a problem sending the command to the program問題
- bash scp command not found的解決方法
- GridView中動態(tài)設(shè)置CommandField是否可用或可見的小例子
- document.execCommand()的用法小結(jié)
- pip 錯誤unused-command-line-argument-hard-error-in-future解決辦法
- 在RowCommand事件中獲取索引值示例代碼
- ON_COMMAND_RANGE多個按鈕響應(yīng)一個函數(shù)的解決方法
- C#命令模式(Command Pattern)實例教程
- ASP基礎(chǔ)知識Command對象講解
相關(guān)文章
Asp.Net使用Bulk實現(xiàn)批量插入數(shù)據(jù)
這篇文章主要介紹了Asp.Net使用Bulk實現(xiàn)批量插入數(shù)據(jù)的方法,對于進行asp.net數(shù)據(jù)庫程序設(shè)計非常有借鑒價值,需要的朋友可以參考下2014-09-09ASP.NET MVC從控制器傳遞數(shù)據(jù)到視圖的四種方式詳解
本篇文章主要介紹了ASP.NET MVC從控制器傳遞數(shù)據(jù)到視圖的四種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01WPF實現(xiàn)ScrollViewer滾動到指定控件處
這篇文章主要為大家詳細介紹了WPF實現(xiàn)ScrollViewer滾動到指定控件處,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06