获取Repeter的Item和ItemIndex/CommandArgument实现思路与代码
程序员文章站
2024-03-04 15:44:59
首先看看效果: repeater控件,放在itemtemplate内的铵钮onclick之后,获取repeater的item,itemindex,commandargume...
首先看看效果:
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
上一篇: Android编程实现提取网址链接的方法