在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. Java实现常见排序算法

    常见的排序算法有冒泡排序.选择排序.插入排序.堆排序.归并排序.快速排序.希尔排序.基数排序.计数排序,下面通过Java实现这些排序 1.冒泡排序 package com.buaa; import j ...

  2. PhalGo-介绍

    PhalGo-介绍 phalgo是一个Go语言的一体化开发框架,主要用于API开发应为使用ECHO框架作为http服务web程序一样可以使用,牛顿曾经说过"如果我比别人看得远,那是因为我站在 ...

  3. PHP利用超级全局变量$_POST来接收表单数据。

    利用$_POST超级全局变量接收表单的数据,然后利用echo输出到页面. 下面是代码: <!doctype html> <html> <head> <titl ...

  4. Restart-ServiceEx.psm1

    详细描述 利用WMI的Win32_Service类重启指定计算机上的服务. Restart-ServiceEx cmdlet 通过WMI的Win32_Service类向指定计算机(ComputerNa ...

  5. PowerDesigner15的安装和破解

    一.PowerDesigner15的安装 运行安装包,出现如下安装界面

  6. rabbitmq pika connection closed

    You are here: Home / rabbitmq pika connection closed rabbitmq pika connection closed By lijiejie on  ...

  7. excel中VBA对多个文件的操作

    添加引用 "Scripting.FileSystemObject" (Microsoft Scripting Runtime) '用于操作文件.目录 Sub 数据整理部分() ' ...

  8. 1 加到 100 的 时间复杂度 C#.

    //1 加到 100 的 时间复杂度: ; ; ; i <= n; i++){ sum += i; } T() = ; //Initialize 'n'. T() = ; //Initializ ...

  9. 测试Kettle在增量更新的时候是否支持级联——不支持(基于mysql)

    由于LZ很像整理一下如何使用Kettle对一张表进行增量更新的时候, 同时实现对数据库中的其他表也可以实现相关的更新操作. 第一种方法想使用触发器,即在相应的hop之间添加SQL脚本step然后在st ...

  10. xml中报错,验证是否是xml报错

    1.xml中写入sql有时报错,例如有大于号小于号,要用<![CDATA[                  ]]>扩起来 2.验证xml有错的方式,以浏览器方式打开,如果正常打开,无错. ...