CheckBoxList 前台 获取 DataValueField



两个项目

都是后台绑定数据到CheckBoxList 上,前台生成的不一样

一个有value属性,一个没有。。对照了半天没啥区别。


web.config中有

<pages controlRenderingCompatibilityVersion="3.5">的时候,不生成 value,

没有 controlRenderingCompatibilityVersion参数时,生成value.


要么去掉参数,要么

参考:http://blog.csdn.net/jxqvip/article/details/6942037

  foreach (ListItem item in CheckBoxList1.Items)

        {

            item.Attributes.Add("val", item.Value);

        }

     在绑定值过后就把value值同时再绑定下,这样绑定后前台就显示出value值了(后来发现刷新页面value值是会丢失的,怎么办,你懂的!)

      前台代码如下:

                   <span val="1001"><input id="dlWater_0" type="checkbox" name="dlWater$0" /><label for="dlWater_0">PH</label></span>

        发现最外面多个span,并且把value值以val的形式给显示出来了,这样就好办多了。

        又查过资料发现jquery可以这样取值

                       $("input[type='checkbox']").click(function(){

           var lvalue=$(this).parent('span').attr("val");//获取value值

           var lname=$(this).next().text();//获取text值

                        }

          这样就能够在前台获取到value值了。




google发现。原来是配置的问题。

参考:http://stackoverflow.com/questions/180245/where-are-the-datavaluefield-values-for-a-checkboxlist-stored



I finally have the answer I've been looking for!

The asp.net CheckboxList control does in fact render the value attribute to HTML - it has been working for me in a Production site for over a year now! (We ALWAYS have EnableViewState turned off for all our sites and it still works, without any tweaks or hacks)

However, all of a sudden, it stopped working one day, and our CheckboxList checkboxes no longer were rendering their value attribute in HTML! WTF you say? So did we! It took a while to figure this out, but since it had been working before, we knew there had to be a reason. The reason was an accidental change to our web.config!

<pages controlRenderingCompatibilityVersion="3.5">

We removed this attribute from the pages configuration section and that did the trick!

Why is this so? We reflected the code for the CheckboxList control and found this in the RenderItem method:

if (this.RenderingCompatibility >= VersionUtil.Framework40)
{
    this._controlToRepeat.InputAttributes.Add("value", item.Value);
}

Dearest dev brothers and sisters do not despair! ALL the answers I searched for here on Stackexchange and the rest of the web gave erroneous information! As of .Net 4.0 asp.net renders the value attribute of the checkboxes of a CheckboxList:

<input type="checkbox" value="myvalue1" id="someid" />

Perhaps not practically useful, Microsoft gave you the ability to add a "controlRenderingCompatibilityVersion" to your web.config to turn this off by setting to a version lower than 4, which for our purposes is completely unnecessary and in fact harmful, since javascript code relied on the value attribute...

We were getting chk.val() equal to "on" for all our checkboxes, which is what originally alerted us to this problem in the first place (using jquery.val() which gets the value of the checkbox. Turns out "on" is the value of a checkbox that is checked... Learn something every day).



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值