在VS2010中的产生ClientID有几种方式,每个控件或页面有个ClientIDMode属性,可以用来决定产生ClientID的方式,它有AutoIDStaticInheritPredictable,具体区别请在网上查找。默认为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>
&nbsp;</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处理的更多相关文章

  1. 13 年的 Bug 调试经验总结

    在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug.最近,我回顾了我所有的194个条目(从13岁开始),看看有什么经验教训是我可 ...

  2. 13 年的 Bug 调试经验总结(来自蜗牛学院)

    在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug. 最近,我回顾了我所有的194个条目,看看有什么经验教训是我可以学习的.下面 ...

  3. 13 年的 Bug 调试经验总结 【转载】

    在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug.最近,我回顾了我所有的194个条目(从13岁开始),看看有什么经验教训是我可 ...

  4. MySQL 5.7.13 的一个BUG

    mysql今天从5.6切到5.7,在测试环境中,日志是全部打印的,发现打了一个警告: Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for ...

  5. cocos2dx 3.13 etc1 ClippingNode Bug 修正

    void TrianglesCommand::useMaterial() const { if(_stencl){ /*******[solyess] etc1 mask的支持************ ...

  6. Tomcat9.0.13 Bug引发的java.io.IOException:(打开的文件过多 Too many open files)导致服务假死

    问题背景: 笔者所在的项目组最近把生产环境Tomcat迁移到Linux,算是顺利运行了一段时间,最近一个低概率密度的(too many open files)问题导致服务假死并停止响应客户端客户端请求 ...

  7. vs2010取消显示所有文件 崩溃

    这属于vs2010的bug,以下是微软官方对些问题的回应: i am still investigating the problem, but looks like the issue is caus ...

  8. 探秘IntelliJ IDEA 13中的版本控制——Subversion 1.8

    IntelliJ IDEA 中引入的重要特性就是版本控制,而在IntelliJ IDEA 13中的体现便是支持最新的Subversion 1.8. 相对于之前版本对Subversion的支持,Subv ...

  9. android-misc-widgets四向(上下左右)抽屉bug修复版--转载

     android-misc-widgets四向(上下左右)抽屉bug修复版 2013-08-04 08:58:13 标签:bug down top panel slidingdrawer 原创作品,允 ...

随机推荐

  1. etrace 跟踪程序函数动态执行流程

    https://github.com/elcritch/etrace 窗口1: 监控窗口,执行监控程序,显示监控结果 [root@monitor example]# pwd /root/etrace- ...

  2. Android中你应该知道的设计模式

    建造者模式 建造者模式最明显的标志就是Build类,而在Android中最常用的就是Dialog的构建,Notification的构建也是标准的建造者模式. 建造者模式很好理解,如果一个类的构造需要很 ...

  3. java web项目中的web.xml标签之context-param

    WEB项目初始化过程: 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>. 接着容器会创建一 ...

  4. 10.30 afternoon

    P76竞赛时间: ????年??月??日??:??-??:?? 题目名称 他 她 它 名称 he she it 输入 he.in she.in it.in 输出 he.out she.out it.o ...

  5. hibernate中有时候复杂删除有时候可以拆分为两个语句

    这个demo是使用原生的sql语句写的,也就是没有调用我在struts中已经写好的公用类common中的增删改查功能,所以要开启事务

  6. Web通信中的Get、Post方法

    首先我们要了解Tomcat,Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选. ...

  7. Android 巧妙实现图片和文字上下布局或者左右布局

    最近去了一家新公司,然后开始做新的项目,看其代码发现了一个很巧妙的方法来实现图片在上面文字在下面的布局方式.只需要一个控件——RadioButton. 布局文件很简单,用来展示RadioBUtton的 ...

  8. SQL Server 表字段值转换成字段名称(二)

    上次写了个比较简单的只有两个字段的例子,经要求在写个  3 个字段的示例 ,贴上来与大家共勉一下   如果你们有更好的方法,提供一下, 感激不尽. 示例如下: /*--drop table temp_ ...

  9. oracle备份表

    oracle与sql单表备份的区别 (     oracle中备份表: create table 备份表名 as select * from 原表 sql server中备份表: select * i ...

  10. http错误代码含义大全详解

    http 错误代码表 所有 HTTP 状态代码及其定义.  代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理, ...