zoukankan
html css js c++ java
asp.net动态生成控件及访问控件
asp.net动态生成控件的方法:
1.在aspx页面放一个Panel控件
2.代码如下:
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
if
(Session[
"
p_userid
"
]
==
null
||
Session[
"
p_loginName
"
]
==
null
||
Session[
"
p_created
"
]
==
null
)
{
Response.Write(
"
<script>alert('请先登陆');location.href='../loginProc/login.aspx';</script>
"
);
Response.End();
}
else
{
Panel1.Controls.Add(
new
LiteralControl(
"
<table>
"
));
Panel1.Controls.Add(
new
LiteralControl(
"
\n
"
));
SqlCommand cmd
=
new
SqlCommand(
"
select infos.* from infos,infoGroups where infos.infoGroup_id = infoGroups.id and infoGroups.title='联系方式' order by infos.pri
"
,connPartner);
SqlDataAdapter da
=
new
SqlDataAdapter();
DataSet ds
=
new
DataSet();
da.SelectCommand
=
cmd;
da.Fill(ds,
"
infos
"
);
for
(
int
i
=
0
;i
<
ds.Tables[
0
].Rows.Count;i
++
)
{
Panel1.Controls.Add(
new
LiteralControl(
"
<tr>
"
));
TableCell cell
=
new
TableCell();
//
创建单元格,也就是第一列
cell.Text
=
ds.Tables[
0
].Rows[i][
"
title
"
].ToString();
//
设置单元格内的文本
Panel1.Controls.Add(cell);
Panel1.Controls.Add(
new
LiteralControl(
"
<td>
"
));
TextBox textBox
=
new
TextBox();
textBox.ID
=
"
t
"
+
ds.Tables[
0
].Rows[i][
"
id
"
].ToString();
Panel1.Controls.Add(textBox);
Panel1.Controls.Add(
new
LiteralControl(
"
</td>
"
));
Panel1.Controls.Add(
new
LiteralControl(
"
<td>
"
));
Label lb
=
new
Label();
lb.ID
=
"
l
"
+
ds.Tables[
0
].Rows[i][
"
id
"
].ToString();
lb.Text
=
"
"
+
ds.Tables[
0
].Rows[i][
"
tip
"
].ToString();
lb.CssClass
=
"
registerInfo
"
;
Panel1.Controls.Add(lb);
Panel1.Controls.Add(
new
LiteralControl(
"
</td>
"
));
Panel1.Controls.Add(
new
LiteralControl(
"
</tr>
"
));
Panel1.Controls.Add(
new
LiteralControl(
"
\n
"
));
}
Button rebt
=
new
Button();
rebt.ID
=
"
redirect
"
;
rebt.Text
=
"
不填了
"
;
rebt.Click
+=
new
EventHandler(rebt_Click);
Panel1.Controls.Add(rebt);
}
}
private
void
rebt_Click(
object
sender, EventArgs e)
{
Response.Redirect(
"
../partnerProc/default.html
"
);
}
访问动态生成控件,可以通过FindControl方法,代码如下:
for
(
int
i
=
0
;i
<
ds.Tables[
0
].Rows.Count;i
++
)
{
Label lb;
lb
=
(Label)Panel1.FindControl(
"
l
"
+
ds.Tables[
0
].Rows[i][
"
id
"
].ToString());
if
(lb
!=
null
)
{
lb.Text
=
"
"
+
ds.Tables[
0
].Rows[i][
"
tip
"
].ToString();
lb.CssClass
=
"
registerInfo
"
;
}
}
查看全文
相关阅读:
orm添加表记录
创建多表模型
包的使用
日志写法
os模块,是通过和操作系统交互进行操作
sys python解释器做交互
软件开发规范
模块 time模块 datatime模块 random模块
装饰器
装饰器进阶
原文地址:https://www.cnblogs.com/nasdaqhe/p/563592.html
最新文章
displaytag 添加超链接
displaytag 简单使用流程
07_Python_迭代器、生成器、装饰器、闭包
06_Python_文件操作(文件夹、文件储存、读取)
05_Python_代码容器(模块)
04_Python_代码容器(类)
Python+Selenium自动化篇-9-定位一组元素,单选框、复选框的选中方法
Python+Selenium自动化篇-8-设置等待三种等待方法
Python+Selenium自动化篇-7-模拟键盘操作
Python+Selenium自动化篇-6-模拟鼠标操作
热门文章
03_Python_代码容器(函数)
Python+Selenium自动化篇-4-清空输入框、输入内容、点击按钮
CBV+装饰器
cookie和session
后台验证插件forms(基于ajax传输数据)
django分页器
使用ajax进行前后台的数据交互
orm聚合查询、分组查询、F查询和Q查询
ORM字段 ORM字段参数 关系字段参数
orm查询
Copyright © 2011-2022 走看看