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
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
查看全文
相关阅读:
SDOI2008]仪仗队
洛谷P1414 又是毕业季II
P3865 【模板】ST表
[HAOI2007]理想的正方形
noip 2011 选择客栈
[AHOI2009]中国象棋
洛谷P3387 【模板】缩点
[SCOI2005]最大子矩阵
[CQOI2009]叶子的染色
LibreOJ #116. 有源汇有上下界最大流
原文地址:https://www.cnblogs.com/yjmyzz/p/1011808.html
最新文章
3ds max学习笔记-- 灯光调节
3ds max学习笔记(十六)-- 摄像机
3ds max学习笔记(十五)-- 二维图形的操作
3ds max学习笔记(十四)-- (FFD自由变形)
资源从3ds max导入UE4问题
3ds max学习笔记(十三)-- (锥化,扭曲,晶格)
3ds max学习笔记(十二)-- (弯曲:实例旋转楼梯)
3ds max学习笔记(十一)-- 修改器
3ds max学习笔记(十)-- 实例操作(镜像和对齐)
Holedox Eating HDU4302 模拟
热门文章
HDU 4300 Clairewd’s message
Hero HDU4310 贪心
Stealing Harry Potter's Precious BFS+DFS
B. Code For 1 分治
Weakness and Poorness CodeForces
C. Painting Fence 分治
4 Values whose Sum is 0
C. Hexadecimal's Numbers
Minimal string 栈 贪心
Noip往年题目整理
Copyright © 2011-2022 走看看