zoukankan
html css js c++ java
如何对动态创建控件进行验证以及在Ajax环境中的使用
首先给一个常规的动态创建控件,并进行验证的代码
[前端aspx代码]
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Test.aspx.cs
"
Inherits
=
"
Test
"
%>
<!
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
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
asp:Table ID
=
"
Table1
"
runat
=
"
server
"
>
</
asp:Table
>
<
asp:Button ID
=
"
btnAddControl
"
runat
=
"
server
"
Text
=
"
动态创建控件
"
OnClick
=
"
btnAddControl_Click
"
/>
<
asp:Button ID
=
"
btnValidator
"
runat
=
"
server
"
Text
=
"
验证动态控件
"
Enabled
=
"
false
"
/>
</
form
>
</
body
>
</
html
>
[后端Cs代码]
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
Test : System.Web.UI.Page
{
private
void
Page_Load(
object
sender, System.EventArgs e)
{
}
protected
void
btnAddControl_Click(
object
sender, EventArgs e)
{
TextBox _TxtBox
=
new
TextBox();
//
动态创建一个TextBox
_TxtBox.ID
=
"
TextBox1
"
;
RequiredFieldValidator _Require
=
new
RequiredFieldValidator();
//
动态创建一个验证控件
_Require.ErrorMessage
=
"
请输入
"
;
_Require.SetFocusOnError
=
true
;
_Require.ControlToValidate
=
_TxtBox.ID;
TableCell Cell
=
new
TableCell();
Cell.Controls.Add(_TxtBox);
Cell.Controls.Add(_Require);
//
将刚才创建的二个控件,加入Cell
TableRow Row
=
new
TableRow();
Row.Controls.Add(Cell);
this
.Table1.Rows.Add(Row);
btnValidator.Enabled
=
true
;
}
}
运行测试,点击"动态创建控件"后,再点击"验证动态控件",验证控件起作用了,一切正常
接下来,我们加入Ajax环境[加入UpdatePanel控件],将前端代码改为:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Test.aspx.cs
"
Inherits
=
"
Test
"
%>
<!
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
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div style="border:solid 2px red"> <%--为突出显示,把UpdatePanel加了一个红色框--%>
<
asp:Table ID
=
"
Table1
"
runat
=
"
server
"
>
</
asp:Table
>
<
asp:Button ID
=
"
btnAddControl
"
runat
=
"
server
"
Text
=
"
动态创建控件
"
OnClick
=
"
btnAddControl_Click
"
/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<
asp:Button ID
=
"
btnValidator
"
runat
=
"
server
"
Text
=
"
验证动态控件
"
Enabled="true"
/>
</
form
>
</
body
>
</
html
>
再次运行,发现没办法再对动态生成的控件进行验证了(也就是说,新创建的验证控件没起作用)
,怎么办呢?难道就这样放弃?经过一番尝试,发现了一个很有趣的解决办法,具体参看以下代码:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Test.aspx.cs
"
Inherits
=
"
Test
"
%>
<!
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
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
asp:ScriptManager ID
=
"
ScriptManager1
"
runat
=
"
server
"
>
</
asp:ScriptManager
>
<
asp:UpdatePanel ID
=
"
UpdatePanel1
"
runat
=
"
server
"
>
<
ContentTemplate
>
<
div style
=
"
border:solid 2px red
"
><%--
为突出显示,把UpdatePanel加了一个红色框
--%>
<
asp:Table ID
=
"
Table1
"
runat
=
"
server
"
>
</
asp:Table
>
<
asp:Button ID
=
"
btnAddControl
"
runat
=
"
server
"
Text
=
"
动态创建控件
"
OnClick
=
"
btnAddControl_Click
"
/>
</
div
>
</
ContentTemplate
>
</
asp:UpdatePanel
>
<div style="display:none">
<asp:TextBox ID="_TxtNeverUsed" runat="server" Text="*"></asp:TextBox>
<asp:RequiredFieldValidator ID="_RequireNeverUsed" runat="server" ErrorMessage="*" ControlToValidate="_TxtNeverUsed"></asp:RequiredFieldValidator>
</div>
<
asp:Button ID
=
"
btnValidator
"
runat
=
"
server
"
Text
=
"
验证动态控件
"
Enabled
=
"
true
"
/>
</
form
>
</
body
>
</
html
>
注意上面蓝色的代码,再次运行,哈哈,居然可以了!
作者:
菩提树下的杨过
出处:
http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
查看全文
相关阅读:
每月碎碎念 | 2019.7
聊聊HTML5中的Web Notification桌面通知
Python的海龟绘图法小知识
面向对象是什么意思?通俗易懂
HTML实体
gcc错误[Error] ld returned 1 exit status
Markdown怎么使用制表符TAB键?为什么TAB失灵了?
力扣题解——2的幂
Jquery中的Ajax
7个你可能不认识的CSS单位
原文地址:https://www.cnblogs.com/yjmyzz/p/1011808.html
最新文章
ASP.NET Core 框架源码地址
ASP.NET Core框架揭秘(持续更新中…)
SQL Server 2014 中新建登录及权限分配【界面版】
sql server 用户创建与权限管理
使用Nuget重新安装packages.config中的组件的方法
IdentityServer4 中文文档与实战
初探Remoting双向通信(四)
高中数学必修二
高中数学必修一
工厂方法-抽象工厂
热门文章
单例
Spring之AOP
IOC注解
Spring发展史
Java两大败笔
&在等号右边和在等号左边
vue watch的高级用法
js对象数组去重
移动端触发touchend后阻止click事件
Jquery中的done() fail() then() $when()到底是什么
Copyright © 2011-2022 走看看