欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gridview行索引获取方法及实现代码

程序员文章站 2024-03-04 17:15:47
insus.net为了演示这个例子,首先准好数据,创建一个类别 cosmetic.vb 复制代码 代码如下: imports microsoft.visualbasic n...
insus.net为了演示这个例子,首先准好数据,创建一个类别
cosmetic.vb
复制代码 代码如下:

imports microsoft.visualbasic
namespace insus.net
public class cosmetic
private _id as integer
private _type as string
private _name as string
private _weight as decimal
private _um as string
public property id as integer
get
return _id
end get
set(value as integer)
_id = value
end set
end property
public property type as string
get
return _type
end get
set(value as string)
_type = 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
public property weight as decimal
get
return _weight
end get
set(value as decimal)
_weight = value
end set
end property
public property um as string
get
return _um
end get
set(value as string)
_um = value
end set
end property
public sub new()
end sub
public sub new(id as integer, type as string, name as string, weight as decimal, um as string)
me._id = id
me._type = type
me._name = name
me._weight = weight
me._um = um
end sub
end class
end namespace

上面创建好的只是一对象,得需用数据填充,让它有血有肉有灵魂。
复制代码 代码如下:

private function getdata() as list(of cosmetic)
dim o as new list(of cosmetic)
dim c as new cosmetic(1, "滋润霜", "玉兰油", 50, "g")
o.add(c)
dim c1 as new cosmetic(2, "滋润霜", "雅诗兰黛", 100, "g")
o.add(c1)
dim c2 as new cosmetic(3, "滋润霜", " 兰蔻", 80, "g")
o.add(c2)
dim c3 as new cosmetic(4, "滋润霜", "欧莱雅", 60, "g")
o.add(c3)
dim c4 as new cosmetic(5, "滋润霜", "芭比波朗", 120, "g")
o.add(c4)
return o
end function

在aspx网页上放一个gridview控件
复制代码 代码如下:

<asp:gridview id="gridviewcosmetic" runat="server" width="300" autogeneratecolumns="false">
<columns>
<asp:templatefield>
<headertemplate>
id
</headertemplate>
<itemtemplate>
<%# eval("id")%>
</itemtemplate>
</asp:templatefield>
<asp:templatefield>
<headertemplate>
type
</headertemplate>
<itemtemplate>
<%# eval("type")%>
</itemtemplate>
</asp:templatefield>
<asp:templatefield>
<headertemplate>
name
</headertemplate>
<itemtemplate>
<%# eval("name")%>
</itemtemplate>
</asp:templatefield>
<asp:templatefield>
<itemstyle horizontalalign="right" />
<headertemplate>
weight
</headertemplate>
<itemtemplate>
<%# eval("weight")%>
</itemtemplate>
</asp:templatefield>
<asp:templatefield>
<headertemplate>
um
</headertemplate>
<itemtemplate>
<%# eval("um") %>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>

当然得对这个控件,进行数据绑定,引用命名空间imports insus.net
复制代码 代码如下:

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.gridviewcosmetic.datasource = getdata()
me.gridviewcosmetic.databind()
end sub

接下来,我们开始演示,在gridview控件最后一列,添加一列,选择列:
复制代码 代码如下:

<asp:templatefield>
<itemtemplate>
<asp:linkbutton id="linkbutton1" runat="server" text="选择" onclientclick="return getselectedrow(this)" />
</itemtemplate>
</asp:templatefield>

上面html代码中,有一个onclientclick="return getselectedrow(this)" 客户端事件。
复制代码 代码如下:

<script type="text/javascript">
function getselectedrow(obj) {
var row = obj.parentnode.parentnode;
var rowindex = row.rowindex - 1;
alert("你选择的行索引是:" + rowindex);
return false;
}
</script>

动画演示
gridview行索引获取方法及实现代码