13. vs2010 ClientID bug处理
在VS2010中的产生ClientID有几种方式,每个控件或页面有个ClientIDMode属性,可以用来决定产生ClientID的方式,它有AutoID,Static,Inherit,Predictable,具体区别请在网上查找。默认为AutoID.
但在VS2010中产生的ClientID是有bug.即产生的ClientID和客户端产生的HTML的ID是不一样的,因此在用js或jQuery的函数中要调用指定ID的元素时系统提示不存在此元素。
处理思路:
由于每个元素产生的ClientID是唯一的,我们可以给要用到的元素设定一个或多个属性,这此属性也是唯一的,这样可以在js中用这些属性来找到此元素并操作它。
示例:
1.在页面中有三个输入框,单价、数量、总金额,当离开单价框或数量框时,会自动在总金额中显示单价*数量的值。因此我们可以建立数据表:id(自增),price(单价),count(数量),Amount(金额).
2.由于要在新增是就应该建立元素的属性,因此用listview 的ItemCreated事件,在这里面注册每个元素属性,并注册它们的事件。
3.html源码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ClientIDBug.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/jquery-1.10.1.js" type="text/javascript"></script>
<script type="text/javascript">
function onamount(priceid, countid, amountid) {
var price = $("input[myid=" + priceid + "]").val();
var count = $("input[myid=" + countid + "]").val();
var iprice = parseFloat(price);
var icount = parseInt(count, 10);
if (!isNaN(iprice) & !isNaN(count)) {
$("input[myid=" + amountid + "]").val(iprice * icount);
} };
</script>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DeleteMethod="Delete" InsertMethod="Insert"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="ClientIDBug.DAL.DataSetPriceTableAdapters.T_PriceTableAdapter"
UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="price" Type="Decimal" />
<asp:Parameter Name="count" Type="Int32" />
<asp:Parameter Name="amount" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="price" Type="Decimal" />
<asp:Parameter Name="count" Type="Int32" />
<asp:Parameter Name="amount" Type="Decimal" />
<asp:Parameter Name="Original_id" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="id"
DataSourceID="ObjectDataSource1" InsertItemPosition="LastItem"
onitemcreated="ListView1_ItemCreated" onitemdatabound="ListView1_ItemDataBound"> <EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
未返回数据。</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
</td>
<td>
</td>
<td>
<asp:TextBox ID="priceTextBox" runat="server" Text='<%# Bind("price") %>' />
</td>
<td>
<asp:TextBox ID="countTextBox" runat="server" Text='<%# Bind("count") %>' />
</td>
<td>
<asp:TextBox ID="amountTextBox" runat="server" Text='<%# Bind("amount") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
</td>
<td>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
</td>
<td>
<asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
</td>
<td>
<asp:Label ID="countLabel" runat="server" Text='<%# Eval("count") %>' />
</td>
<td>
<asp:Label ID="amountLabel" runat="server" Text='<%# Eval("amount") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
</th>
<th runat="server">
id</th>
<th runat="server">
price</th>
<th runat="server">
count</th>
<th runat="server">
amount</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate> </asp:ListView> </div>
</form>
</body>
</html>
4. ListView的ItemCreate源码
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.InsertItem)
{
TextBox priceTextBox = (TextBox)e.Item.FindControl("priceTextBox");
TextBox countTextBox = (TextBox)e.Item.FindControl("countTextBox");
TextBox amountTextBox = (TextBox)e.Item.FindControl("amountTextBox"); priceTextBox.Attributes["myid"] = priceTextBox.ClientID;
countTextBox.Attributes["myid"] = countTextBox.ClientID;
amountTextBox.Attributes["myid"] = amountTextBox.ClientID; string amountfun=string.Format("onamount('{0}','{1}','{2}')",priceTextBox.ClientID,countTextBox.ClientID,amountTextBox.ClientID); priceTextBox.Attributes["onblur"] = amountfun;
countTextBox.Attributes["onblur"] = amountfun;
}
}
13. vs2010 ClientID bug处理的更多相关文章
- 13 年的 Bug 调试经验总结
在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug.最近,我回顾了我所有的194个条目(从13岁开始),看看有什么经验教训是我可 ...
- 13 年的 Bug 调试经验总结(来自蜗牛学院)
在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug. 最近,我回顾了我所有的194个条目,看看有什么经验教训是我可以学习的.下面 ...
- 13 年的 Bug 调试经验总结 【转载】
在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug.最近,我回顾了我所有的194个条目(从13岁开始),看看有什么经验教训是我可 ...
- MySQL 5.7.13 的一个BUG
mysql今天从5.6切到5.7,在测试环境中,日志是全部打印的,发现打了一个警告: Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for ...
- cocos2dx 3.13 etc1 ClippingNode Bug 修正
void TrianglesCommand::useMaterial() const { if(_stencl){ /*******[solyess] etc1 mask的支持************ ...
- Tomcat9.0.13 Bug引发的java.io.IOException:(打开的文件过多 Too many open files)导致服务假死
问题背景: 笔者所在的项目组最近把生产环境Tomcat迁移到Linux,算是顺利运行了一段时间,最近一个低概率密度的(too many open files)问题导致服务假死并停止响应客户端客户端请求 ...
- vs2010取消显示所有文件 崩溃
这属于vs2010的bug,以下是微软官方对些问题的回应: i am still investigating the problem, but looks like the issue is caus ...
- 探秘IntelliJ IDEA 13中的版本控制——Subversion 1.8
IntelliJ IDEA 中引入的重要特性就是版本控制,而在IntelliJ IDEA 13中的体现便是支持最新的Subversion 1.8. 相对于之前版本对Subversion的支持,Subv ...
- android-misc-widgets四向(上下左右)抽屉bug修复版--转载
android-misc-widgets四向(上下左右)抽屉bug修复版 2013-08-04 08:58:13 标签:bug down top panel slidingdrawer 原创作品,允 ...
随机推荐
- uva 11440 - Help Tomisu(欧拉功能)
题目链接:uva 11440 - Help Tomisu 题目大意:给定n和m,求从2~n.中的数x.要求x的质因子均大于m.问说x有多少个.答案模上1e9+7. 解题思路: (1)n!=k∗m!(n ...
- 【POJ3182】The Grove BFS 最短路径周围
意甲冠军:给定一个N*M图.,间'X'代表树木(树木必须汇集到森林,非分离),然后,'.'它代表的空间.'*'它代表的起点.现在它需要从起点.一圈,最后回到起点,所经过最少点数. 题目中给的'+'就是 ...
- systemtap分析软raid io拆分问题
http://www.sysnote.org/2014/05/01/systemtap-analysis-mdraid-io/
- Creating Custom Shadows ——创建自定义shadow
Custom shadows are a Robolectric feature that allows you to make targeted changes in the way Android ...
- Ubuntu16.04LTS安装
1. 制作u盘启动盘 下载ubuntu-16.04-desktop-amd64.iso文件后,使用u盘启动盘制作工具:Win32DiskImager(14.04LTS后都需要用到这工具制作:https ...
- oracle多表关联删除数据表记录方法
oracle多表关联删除的两种方法 第一种使用exists方法 delete from tableA where exits ( select 1 from tableB Where tableA.i ...
- android MediaCodec 音频编解码的实现——转码
原文地址:http://blog.csdn.net/tinsanmr/article/details/51049179 从今天开始 每周不定期更新博客,把这一周在工作与学习中遇到的问题做个总结.俗话说 ...
- C++ hello world
日文版本的vs 2008 , 在 < 新建 里面先创建一个项目 然后点击项目去创建一个C++的主启动文件 选择创建的文件类型 然后在文件里面写入代码 #include<iostream&g ...
- c#正则表达式采集数据
protected void Page_Load(object sender, EventArgs e){ StringBuilder MyStringBuilder = new StringBuil ...
- angularjs kindEditor 中自定义按钮 弹出dialog
1.angular-kindeditor.js 第38行左右加 editorConfig.items = ["placehoder"]; 2.en.js 第234行 placeho ...