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 原创作品,允 ...
随机推荐
- Android 仿网易新闻v3.5:上下滑动的引导页
版权声明:本文为博主原创文章,未经博主允许不得转载. 在很多天气或者新闻的应用中,我们都能看到一些字幕滚动的效果,最简单的实现为跑马灯效果,用系统提供的属性即可实现. 复杂一些的就需要自己去用自定义控 ...
- android 53 ContentProvider内容提供者
ContentProvider内容提供者:像是一个中间件一样,一个媒介一样,可以以标准的增删改差操作对手机的文件.数据库进行增删改差.通过ContentProvider查找sd卡的音频文件,可以提供标 ...
- android 13 5种click事件不同实现方式 比较
第一种:不便于管理. <Button android:id="@+id/btn_Gridlayout" android:layout_width="match_pa ...
- Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用
在HTML5游戏开发中,js可以使用Event对象的addEventListener(添加事件监听).dispatchEvent(触发事件)实现监听机制,如果在coocos2d-x中,去实现这种机制该 ...
- win7 下配置 java 环境变量
首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入“计算机 ...
- WPF TextElement内容模型简介(转)
本内容模型概述描述了 TextElement 支持的内容. Paragraph 类是 TextElement 的类型. 内容模型描述哪些对象/元素可以包含在其他对象/元素中. 本概述汇总了派生自 Te ...
- 使用Convert 类和Parse方法将字符串转换为数值类型
//用Parse方法将字符串转换为数值类型; long num=Int64.Parse(args[2]) //用别名为Int64c#类型long; long num=long.Parse(args[2 ...
- 【SPOJ839】最优标号
[题目描述] 给你一张无向图G(V,E).每个顶点都有一个标号,它是一个[0,2^31-1]内的整数.不同的顶点可能会有相同的标号. 对每条边(u,v),我们定义其费用cost(u,v)为u的标号与v ...
- 工厂方法模式(Factory Method)
1.本质:延迟到子类来选择实现 2.示意图: 3.主要功能: 让父类在不知道具体实现的情况下,完成自身功能的调用 类似于注入 4.备注: 1.工厂方法中,通常父类是一个抽象类,里面包含创建对象的抽象工 ...
- 解决 dyld: Library not loaded:Reason: image not found
在使用第三方framework时,直接把framework拖到项目中,运行时报错: dyld: Library not loaded: @rpath/ZipZap.framework/ZipZap R ...