zoukankan
html css js c++ java
动态添加控件并获取其值
google
注意:
1.aspx页面<%Page%>里必须添加
EnableViewState="true"
,
使动态添加的控件状态可保存
2.动态添加的控件最好
放在容器上
(这里用Panel这个容器)
3.必须
设置动态控件的ID
,否则获取不到该控件
建议:运用
Asp.net Ajax
会有
更好的用户体验效果
例子:
test2.aspx:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Test2.aspx.cs
"
Inherits
=
"
Test2
"
Title
=
"
Untitled Page
"
EnableViewState
="true"
%>
<
asp:Panel ID
=
"
Panel2
"
runat
=
"
server
"
Height
=
"
50px
"
Width
=
"
446px
"
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
"
Name1:
"
></
asp:Label
>
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
></
asp:TextBox
>
<
asp:Label ID
=
"
Label2
"
runat
=
"
server
"
Text
=
"
Address1:
"
></
asp:Label
>
<
asp:TextBox ID
=
"
TextBox2
"
runat
=
"
server
"
Width
=
"
149px
"
></
asp:TextBox
><
br
/>
</
asp:Panel
>
<
asp:Button ID
=
"
btnAddAjax
"
runat
=
"
server
"
Text
=
"
Add
"
OnClick
=
"
btnAddAjax_Click
"
/>
<
asp:Button id
=
"
btnShow
"
onclick
=
"
btnShow_Click
"
runat
=
"
server
"
Text
=
"
Show
"
/>
<
asp:Label id
=
"
lblMsg
"
runat
=
"
server
"
>
No Value
</
asp:Label
>
test2.aspx.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
Test2 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(ViewState[
"
txtName2
"
]
!=
null
&&
(
bool
)ViewState[
"
txtName2
"
])
{
CreateMyControls();
}
}
protected
void
btnAddAjax_Click(
object
sender, EventArgs e)
{
CreateMyControls();
}
protected
void
btnShow_Click(
object
sender, EventArgs e)
{
if
(ViewState[
"
txtName2
"
]
!=
null
)
{
TextBox txtName2
=
Panel2.FindControl(
"
txtName2
"
)
as
TextBox;
if
(txtName2
!=
null
)
lblMsg.Text
=
"
Name2=
"
+
txtName2.Text;
}
if
(ViewState[
"
txtAddr2
"
]
!=
null
)
{
TextBox txtAddr2
=
Panel2.FindControl(
"
txtAddr2
"
)
as
TextBox;
if
(txtAddr2
!=
null
)
lblMsg.Text
+=
"
Addr2=
"
+
txtAddr2.Text;
}
}
private
void
CreateMyControls()
{
Label lblName2
=
new
Label();
lblName2.Text
=
"
Name2:
"
;
TextBox txtName2
=
new
TextBox();
//
设置ID,否则不能获取到值
txtName2.ID
= "txtName2"
;
//
可视状态,否则PostBack后动态添加的控件会不见
ViewState[
"txtName2"]= true
;
Label lblAddr2
=
new
Label();
lblAddr2.Text
=
"
Addr2:
"
;
查看全文
相关阅读:
python基础--二分查找
python基础--字典
python基础--列表和元组
python基础--基本数据类型的概述
python基础--循环
python基础--变量和基础数据类型
Python2与Python3区别
project euler之最大的回文产品
project euler之最大的素因子
project euler之甚至斐波那契数字(Even Fibonacci numbers)
原文地址:https://www.cnblogs.com/sinkzephyr/p/862626.html
最新文章
python基础 day16 常用模块
python基础 day15 自定义模块、常用模块random
python基础 day14 装饰器
python基础 day13 匿名函数、内置函数、闭包
python基础 day12 生成器、生成器表达式、列表字典集合推导式
python基础 day11 默认参数和局部作用域的坑、global和nonlocal方法、函数名的应用、格式化输出的另一种方式、可迭代对象和迭代器
python基础 day10 函数式编程、名称空间、作用域、高阶函数
python基础 day9 函数式编程1
python基础 day8 文件操作
python:practice set copy
热门文章
python:practice function return
python :practice function return
python:practice stdout
python: practice ,for i in range
python:practice shart file --operation object ,article,essay, paper,thesis
python:practice encode decode
python:practice Advanced three-level menu
python: practice three-level manu
python: practice string method
hash算法的移到面试题
Copyright © 2011-2022 走看看