zoukankan
html css js c++ java
(补充)移除动态添加的控件
好像问动态添加控件的人很多,问题大多集中
(1)动态添加的按钮不能提交
(2)动态添加的文本框取不到值
(3)动态添加的控件页面刷新后消失
(4)动态添加的控件页面刷新重复添加
(5)动态添加的控件不知道怎么移除
问题百出,但是其实这些都是非常简单的,可能疏忽了一点两点,补充以前写的2篇文章(以前写的比较乱)
这个页面实现:
点击添加按钮-》添加一个文本框一个提交按钮-》点击提交按钮输出文本框值
点击删除按钮(就是前面那个添加按钮)-》移除文本框和提交按钮
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
namespace
csdn2
{
/**/
///
<summary>
///
WebForm65 的摘要说明。
///
</summary>
public
class
WebForm65 : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.Button Button1;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
if
(Page.IsPostBack)
{
if
(ViewState[
"
adduc
"
]
!=
null
)
{
adduc();
}
}
}
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.Button1.Click
+=
new
System.EventHandler(
this
.Button1_Click);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
if
(ViewState[
"
adduc
"
]
==
null
)
{
adduc();
ViewState[
"
adduc
"
]
=
1
;
}
else
{
deluc();
ViewState[
"
adduc
"
]
=
null
;
}
}
private
void
Button2_Click(
object
sender, System.EventArgs e)
{
TextBox t
=
(TextBox)Page.FindControl(
"
t
"
);
if
(t
!=
null
)Response.Write(t.Text);
}
public
void
adduc()
{
TextBox t
=
new
TextBox();
t.ID
=
"
t
"
;
Page.Controls[
1
].Controls.Add(t);
this
.Button1.Text
=
"
删除
"
;
Button b
=
new
Button();
b.Text
=
"
提交
"
;
b.ID
=
"
b
"
;
b.Click
+=
new
System.EventHandler(
this
.Button2_Click);
Page.Controls[
1
].Controls.Add(b);
}
public
void
deluc()
{
Page.Controls[
1
].Controls.Remove(Page.FindControl(
"
t
"
));
Page.Controls[
1
].Controls.Remove(Page.FindControl(
"
b
"
));
this
.Button1.Text
=
"
添加
"
;
}
}
}
欢迎大家阅读我的极客时间专栏
《Java业务开发常见错误100例》
【全面避坑+最佳实践=健壮代码】
查看全文
相关阅读:
python定制类详解
python格式化
python3和2的区别
深度优先和广度优先遍历
python偏函数
python匿名函数
android 应用能够安装在什么地方
C语言文件操作函数
病毒木马查杀实战第026篇:“白加黑”恶意程序研究(上)
函数指针
原文地址:https://www.cnblogs.com/lovecherry/p/152455.html
最新文章
[DeeplearningAI笔记]卷积神经网络2.2经典网络
[DeeplearningAI笔记]卷积神经网络1.9-1.11池化层/卷积神经网络示例/优点
[DeeplearningAI笔记]卷积神经网络1.6-1.7构造多通道卷积神经网络
锐捷客户端-您不在许可范围中,请确认您的权限
[DeeplearningAI笔记]卷积神经网络1.4-1.5Padding与卷积步长
[DeeplearningAI笔记]卷积神经网络1.2-1.3边缘检测
Tensorboard教程:高维向量可视化
Tensorboard教程:监控指标可视化
使用tqdm组件构造程序进度条
Tensorboard教程:显示计算图中节点信息
热门文章
Tensorflow命名空间与计算图可视化
使用L2正则化和平均滑动模型的LeNet-5MNIST手写数字识别模型
持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型
Tensorflow模型变量保存
Tensorflow BatchNormalization详解:4_使用tf.nn.batch_normalization函数实现Batch Normalization操作
Tensorflow BatchNormalization详解:3_使用tf.layers高级函数来构建带有BatchNormalization的神经网络
Linux写时拷贝技术(copy-on-write)
redis性能提升
Linux,Windows中的相对路径、绝对路径
python地址解析经纬度,城市
Copyright © 2011-2022 走看看