数据绑定控件ListView事件ItemCreated的Bug
2012-12-31 23:31
597 查看
习惯性的用TextBox.ClientID取来服务端渲染到客户端的ID,而在ListView的ItemCreated事件中却怎么也取不到。
我的一个WareAllMoneyTextBox控件,
ClientID="ctrl6_WareAllMoneyTextBox“ 而渲染到客户端变成了id="ListView1_WareAllMoneyTextBox" 。
目前发现的解决方法有三个:
方法一:
仔细观察两个ID会发现是有相同之处的,我通过代码生成了数个类似代码,研究结果也是一样的。当需要在服务端设置客户端的Js或者样式等等的时候,可以后台直接使用ListView1_加上控件的后台名字,当然这样做当控件多的时候会很麻烦。在不同的ListView中前面加的东西应该也是不一样的。
方法二:
直接使用Attributes再生成一个Id到客户端去,然后在客户端的均对新生成的ID的控件做操作。
textbox1.Attributes["myId"] = textbox1.ClientID;
textbox2.Attributes["onblur"] = "$('input[myId="+textbox1.ClientID+"').text('test')";
方法三:
完全用JQuery的next之类的方法进行定位不依赖于ID。
微软官方回复:
There is a bug in how System.Web.UI.Control caches its UniqueID if it has been accessed before its NamingContainer has been added to the control tree. This is exactly the bug you are seeing -- your custom control accesses its UniqueID property before its parent GenericWebPart (a NamingContainer) has been added to the control tree.
Unfortunately, due to performance and compatibility concerns, we are unable to fix the root cause of this issue in System.Web.UI.Control. Here are the workaround that may help your scenario:
我的一个WareAllMoneyTextBox控件,
ClientID="ctrl6_WareAllMoneyTextBox“ 而渲染到客户端变成了id="ListView1_WareAllMoneyTextBox" 。
目前发现的解决方法有三个:
方法一:
仔细观察两个ID会发现是有相同之处的,我通过代码生成了数个类似代码,研究结果也是一样的。当需要在服务端设置客户端的Js或者样式等等的时候,可以后台直接使用ListView1_加上控件的后台名字,当然这样做当控件多的时候会很麻烦。在不同的ListView中前面加的东西应该也是不一样的。
方法二:
直接使用Attributes再生成一个Id到客户端去,然后在客户端的均对新生成的ID的控件做操作。
textbox1.Attributes["myId"] = textbox1.ClientID;
textbox2.Attributes["onblur"] = "$('input[myId="+textbox1.ClientID+"').text('test')";
方法三:
完全用JQuery的next之类的方法进行定位不依赖于ID。
微软官方回复:
There is a bug in how System.Web.UI.Control caches its UniqueID if it has been accessed before its NamingContainer has been added to the control tree. This is exactly the bug you are seeing -- your custom control accesses its UniqueID property before its parent GenericWebPart (a NamingContainer) has been added to the control tree.
Unfortunately, due to performance and compatibility concerns, we are unable to fix the root cause of this issue in System.Web.UI.Control. Here are the workaround that may help your scenario:
相关文章推荐
- android ListView控件操作绑定数据、单击事件
- 数据绑定控件ListView事件
- ListView应用------ListView控件显示List集合中的数据,并对Item响应选择事件(法一)
- Android布局设计之ExpandableList绑定XML数据构成级联、item布局页面的控件查找及配置child事件,自定义适配显示内容
- Repeater 的操作,数据的显示,绑定,动态取控件中的值和Repeater1_ItemCommand事件的相关用法
- Android开发:ListView控件:给Item绑定了点击事件,却点击无效
- ListView应用------ListView控件显示List集合中的数据,并对Item响应选择事件(法二)
- Repeater 的操作,数据的显示,绑定,动态取控件中的值和Repeater1_ItemCommand事件的相关用法
- 完美解决ListView中事件ItemCreated中使用ClientID导致插入数据失败
- 数据绑定控件的ItemDataBound事件中怎么得到数据库字段的值
- gridview绑定checkbox控件后,通过单击CheckedChanged事件获取当前选择行的数据
- ListView点击事件失效(item里面有button按钮控件)解决方法
- ListView的item中EditText编辑(或者其他控件)修改本行数据
- 关于ListView中控件点击事件与Item点击事件冲突的问题
- 监听listview中item有控件点击事件的变化,更新listview
- 解决Listview的item和里面控件点击事件冲突
- ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
- DataItem,gridview,repeater数据控件数据绑定
- Asp.net--数据绑定控件ListView
- Android ListView下拉加载更多,http请求加载数据,Item点击事件