您的位置:首页 > 产品设计 > UI/UE

CheckBoxList 前台 获取 DataValueField

2014-04-28 12:00 232 查看

两个项目

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

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

web.config中有

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

没有 controlRenderingCompatibilityVersion参数时,生成value.

要么去掉参数,要么

参考:/article/10640016.html

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

3down vote
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).



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: