当前位置: 首页 > news >正文

获取GridView行索引

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

 
上面创建好的只是一对象,得需用数据填充,让它有血有肉有灵魂。

View Code
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控件:

View Code
 <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

View Code
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控件最后一列,添加一列,选择列:

1 <asp:TemplateField>
2     <ItemTemplate>
3         <asp:LinkButton ID="LinkButton1" runat="server" Text="选择" OnClientClick="return GetSelectedRow(this)" />
4     </ItemTemplate>
5 </asp:TemplateField>


上面html代码中,有一个OnClientClick="return GetSelectedRow(this)" 客户端事件。

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


动画演示:

 

转载于:https://www.cnblogs.com/insus/archive/2013/01/13/2858768.html

相关文章:

  • win32 多线程基础
  • javascript 跳转页面
  • Java 泛型(一) 泛型使用基础
  • Codeforces 264B Good Sequences ★ (分解素因子+DP)
  • Unity(五):使用场景Ⅱ:用于单例模式
  • 【ZZ】9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路...
  • 密码绑定至密码文本框中
  • 使用PowerDesigner建立数据库模型
  • outerHTML兼容方法(jquery)
  • 解决compilation debug=true targetFramework=4.0 问题
  • Python学习笔记《Python核心编程》第13章 面向对象编程
  • GDI+ 图片高亮处理
  • int 反射到未知的 Enum , 使用 Enum.ToObject
  • USACO 3.1.1 Agri-Net
  • 关于SharePoint 2010 SP1
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • Android Volley源码解析
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • node-glob通配符
  • 闭包--闭包之tab栏切换(四)
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 类orAPI - 收藏集 - 掘金
  • 驱动程序原理
  • 用简单代码看卷积组块发展
  • 与 ConTeXt MkIV 官方文档的接驳
  • # 数据结构
  • #Linux(Source Insight安装及工程建立)
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (二)换源+apt-get基础配置+搜狗拼音
  • (翻译)terry crowley: 写给程序员
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (四)鸿鹄云架构一服务注册中心
  • (一)基于IDEA的JAVA基础1
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)memcache、redis缓存
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • (转)重识new
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .net 简单实现MD5
  • .net专家(张羿专栏)
  • @angular/cli项目构建--http(2)
  • [ CTF ]【天格】战队WriteUp- 2022年第三届“网鼎杯”网络安全大赛(青龙组)
  • []FET-430SIM508 研究日志 11.3.31
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [20180224]expdp query 写法问题.txt
  • [30期] 我的学习方法
  • [Android]使用Android打包Unity工程