当前位置: 首页 > 图文教程 > 网络编程 > Javascript > jQuery生成asp.net服务器控件的代码

Javascript
js中几种去掉字串左右空格的方法
判断两种颜色值是否为相似颜色
关于键盘事件中keyCode、which和charCode 的兼容性测试
提高 DHTML 页面性能
漂亮的Slider效果类终于封装成功
注释的艺术——JS里直接写HTML,无需转义
用javascript实现无刷新更新数据的详细步骤 asp
Windows Live的@live.com域名注册漏洞 利用代码
只能输入小于最大数且是正整数的脚本
破除网页鼠标右键被禁用的绝招大全
escape、encodeURI、encodeURIComponent等方法的区别比较
URL编码转换,escape() encodeURI() encodeURIComponent()
使用javascript访问XML数据的实例
ie和firefox中img对象区别的困惑
utf8的编码算法 转载
Javascript六种风格的时间显示方式
JavaScript实现禁止后退的方法
从javascript语言本身谈项目实战
通用于ie和firefox的函数 GetCurrentStyle (obj, prop)
javascript实现划词标记划词搜索功能修正版

Javascript 中的 jQuery生成asp.net服务器控件的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 329 ::
收藏到网摘: n/a

jQuery生成asp.net服务器控件 但不幸的是,该服务器控件依然没有起作用,还是用隐藏服务器控件来解决吧 HTML如下
复制代码 代码如下:

<tr>
<td class="leftTd" style="width: 107px">附加金额</td>
<td style="width: 315px"><asp:TextBox ID="txtExtendMoney" Text="0" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="regExtend" runat="server" ControlToValidate="txtExtendMoney" Display="Dynamic" ErrorMessage="格式不正确" ValidationExpression="[1-9]\d*\.\d*|0\.\d*[1-9]\d*|^[1-9]\d*|0"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="reqExtedNo" runat="server" ControlToValidate="txtExtendMoney" Display="Dynamic" ErrorMessage="不可为空"></asp:RequiredFieldValidator></td>
<td class="leftTd">结算方式</td>
<td><asp:DropDownList ID="ddlPayType" runat="server"><asp:ListItem>现金</asp:ListItem><asp:ListItem>银行转账</asp:ListItem></asp:DropDownList></td>
</tr>
<tr>
<td class="leftTd">结算账户</td>
<td colspan="3"><asp:RadioButtonList ID="rdbPayAccountBank" runat="server" RepeatLayout="Flow"></asp:RadioButtonList></td>
</tr>

最后一个RadioButtonList的ListItem为“其他账户",当选中时,其后增加相应的asp.net服务器控件。选择其它时移除该控件。

增加

引入jQuery,然后如下代码

复制代码 代码如下:

/*结算方式*/
$(":radio:last").bind("click",function(){
if($("#txtBankNew").length==0){
$(this).parent().append('<span id="span"><label style="margin-left:6px;margin-right:4px;" for="txtBankNew">开户银行</label><input runat='server' id='txtBankNew' type='text' /><label style="margin-left:6px;margin-right:4px;" for="txtAccountNew">开户账户</label><input type='text' id='txtAccountNew' runat='server' /></span>');
};
$("#txtBankNew").focus().select();
});
$(":radio:not(:last)").bind("click",function(){
if($("#txtBankNew").length>0){
$("#span").remove();
}
});

这里值得注意的是如果append之后的控件为服务器控件,也就是有runat="server"属性的,原先的单引号生成源后会自动变成双引号,并且runat="server"消失。这实际上跟手工在前台书写此DOM结构.net framework处理一致。因此打开此页面源文件可以看到如下

但不幸的是,该服务器控件依然没有起作用……
还是用隐藏服务器控件来解决吧–!