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
"
;
}
}
查看全文
相关阅读:
zzuli 1908
继承 封装 多态 java的三大特性
FZU 2232
zzuli 1079
zzuli 1023
二分图的匹配 hdu 1083
CodeIgniter学习笔记(五)——CI超级对象中的uri
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
CodeIgniter学习笔记(三)——CI中的视图
CodeIgniter学习笔记(二)——CI中的控制器
原文地址:https://www.cnblogs.com/nasdaqhe/p/563592.html
最新文章
POJ 2112 Optimal Milking 【网络流】【flyod】【二分】
POJ 3436 ACM Computer Factory 【网络流】【北大ACM/ICPC竞赛训练】
POJ 2349 Arctic Network 【最小生成树】【北大ACM/ICPC竞赛训练】
洛谷 P2704 炮兵阵地 【状压再状压dp】【北大ACM/ICPC竞赛训练】
POJ 7219 复杂的整数划分问题 【dp】【北大ACM/ICPC竞赛训练】
POJ 1194 Zipper 【dp】【北大ACM/ICPC竞赛训练】
2020年7月10日实验室学术研讨会议
2020年7月1日实验室学术研讨会议
2020年6月11日实验室学术研讨会议
2020年6月24日实验室学术研讨会议
热门文章
2020年6月19日实验室学术研讨会议
2020年6月3日实验室学术研讨会议
2020年5月28日实验室学术研讨会议
2020年5月13日实验室学术研讨会议
2020年4月29日实验室学术研讨会议
2020年4月23日实验室学术研讨会议
zzuli 1902: 985的因子对难题
java 对象作为成员变量
zzuli1895: 985的0-1串难题
zzuli 1907: 小火山的宝藏收益
Copyright © 2011-2022 走看看