zoukankan      html  css  js  c++  java
  • 动态为GridView添加DataKeyNames属性与赋值

    由于程序中,这个DataKeyNames名称是一个动态值,因此在html的GridView不能直接写成这样子asp:GridViewID="GridViewUnitCode"runat="server" DataKeyNames="xxx" ...  也就是说,html中的Gridview不能带DataKeyNames属性,如果写上了,就是写死了。

    在.cs中,Insus.NET是这样解决的。

    为GridView动态添加DataKeyNames属性并赋值,有一点需要注意的,就是它应该是在GridView绑定数据前时进行。下面两种顺序是正确的
    第1种:

     
    第2种:

     下面是先对GridView的数据绑定,然后给Gridview动态添加DataKeyNames属性并赋值,是不正确的:

    它会在更新或是删除时,获取主键时

     GridViewRow gvr = GridViewUnitCode.Rows[e.RowIndex];
     objUnitCode.UnitCode = GridViewUnitCode.DataKeys[e.RowIndex].Value.ToString();


    会出现异常Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

  • 相关阅读:
    基础数据类型
    python2x与python3x区别(30个)更新中。。。
    注释
    常量
    变量
    十、SpringCloud config分布式配置中心
    九、Gateway新一代网关
    八、Hystrix断路器(下)
    八、Hystrix断路器(上)
    七、OpenFeign服务接口调用
  • 原文地址:https://www.cnblogs.com/insus/p/3020327.html
Copyright © 2011-2022 走看看