zoukankan
html css js c++ java
关于CheckBoxList和RadioButtonList的几个问题
其实我一向很少用关于"list"的控件,因为有DataGrid就基本上都搞定了,然而一方面是为了学习,另一方面也可以灵活的应用更多的控件,于是采用了其中的几个控件,但没想到结果令我大失所望。。。。
先看看DataList控件,它的使用最郁闷的是不能在IDE环境里绑定数据,而只能用模板(不知道是不是我没学会)。
<
asp:DataList
id
="DataList1"
runat
="server"
Width
="100%"
Visible
="False"
CssClass
="s_verdana"
RepeatDirection
="Horizontal"
ForeColor
="Black"
BorderColor
="#999999"
BorderStyle
="Solid"
BackColor
="Silver"
CellPadding
="1"
GridLines
="Vertical"
BorderWidth
="1px"
RepeatColumns
="4"
DataKeyField
="c_id"
>
<
SelectedItemStyle
Font-Bold
="True"
ForeColor
="White"
BackColor
="#000099"
></
SelectedItemStyle
>
<
AlternatingItemStyle
BackColor
="#CCCCCC"
></
AlternatingItemStyle
>
<
ItemTemplate
>
<
asp:CheckBox
Runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.c_loginName")%
>
'>
</
asp:CheckBox
>
</
ItemTemplate
>
数据绑定:
this
.DataList1.DataSource
=
m_table.DefaultView;
this
.DataList1.DataKeyField
=
"
c_id
"
;
this
.DataList1.DataBind();
看上去和DataGrid一样不是吗?可问题是:这里我这里使用了CheckBox,也就是说用户到时候要选择其中的部份数据,然后提交回来,结果是:你很难得在DataGrid里取回绑定的数据列。。。。。
再看RadioButtonList:
<
asp:RadioButtonList
id
="RadioButtonList_MsgType"
runat
="server"
Width
="440px"
RepeatDirection
="Horizontal"
CssClass
="s_verdana"
>
<
asp:ListItem
Value
="0"
Selected
="True"
>
All
</
asp:ListItem
>
<
asp:ListItem
Value
="1"
>
All Client
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
All Users
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
Sperical
</
asp:ListItem
>
</
asp:RadioButtonList
>
这里我只使用了静态的数据,因为这里并没有很多的数据显示。但有一点小问题:我想在上面加一个JavaScript事件,用来响应用户操作,于是我在后台添加代码:
private
void
RadioButtonList_MsgType_Load(
object
sender, System.EventArgs e)
{
RadioButtonList m_obj = sender as RadioButtonList;
m_obj.Attributes.Add(
"
onclick
"
,
"
alert()
"
);
}
让我郁闷不以的是:::它并没有在radio添加Onclick事件,查看HTML源代码,差点没把我气死:原来它生成了一个Table,而这个RadioButtonList就是这个Table,而里面的Radio就是行与列了。
<table id="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType" class="s_verdana" onclick="alert()" border="0" style="440px;">
<
tr
>
<
td
><
input
id
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_0"
type
="radio"
name
="Inc_Admin_MsgManager1:Inc_WAVE_Messages1:RadioButtonList_MsgType"
value
="0"
/><
label
for
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_0"
>
All
</
label
></
td
><
td
><
input
id
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_1"
type
="radio"
name
="Inc_Admin_MsgManager1:Inc_WAVE_Messages1:RadioButtonList_MsgType"
value
="1"
/><
label
for
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_1"
>
All Client
</
label
></
td
><
td
><
input
id
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_2"
type
="radio"
name
="Inc_Admin_MsgManager1:Inc_WAVE_Messages1:RadioButtonList_MsgType"
value
="2"
/><
label
for
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_2"
>
All Users
</
label
></
td
><
td
><
input
id
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_3"
type
="radio"
name
="Inc_Admin_MsgManager1:Inc_WAVE_Messages1:RadioButtonList_MsgType"
value
="3"
checked
="checked"
/><
label
for
="Inc_Admin_MsgManager1_Inc_WAVE_Messages1_RadioButtonList_MsgType_3"
>
Sperical
</
label
></
td
>
</
tr
>
</
table
>
这也算了,如果非要这样用也行,可以在它的子控件里再添加事件,这样可是麻烦多了。看看CheckBoxList,原理也一样,也就不多说了。
回到DataList上来,看它如果用CheckBox来取回数据吧,先把所有的子控件都列出来:
private
void
AddMessageRelations()
{
if
(
this
.RadioButtonList_MsgType.SelectedIndex
<
3
)
return
;
CheckBox m_checkBox;
for
(
int
i
=
0
;i
<
this
.DataList1.Items.Count;i
++
)
{
//
m_checkBox = this.DataList1.Items[i].Controls[0] as CheckBox;
//
if(m_checkBox.Checked)
//
{
//
Response.Write(m_checkBox.Text);
//
}
Response.Write(
"
i=
"
+
i.ToString()
+
"
:
"
+
this
.DataList1.Items[i].ToString()
+
"
<br/>
"
);
for
(
int
j
=
0
;j
<
this
.DataList1.Items[i].Controls.Count;j
++
)
{
Response.Write(
"
j=
"
+
j.ToString()
+
"
:
"
+
this
.DataList1.Items[i].Controls[j].ToString()
+
"
<br/>
"
);
}
Response.Write(
"
<br>==================================<br>
"
);
}
}
看看结果:
好了,清楚是怎么回事了就好办了。以下代码取得用户选定CheckBox的ID值。
private
void
AddMessageRelations()
{
if
(
this
.RadioButtonList_MsgType.SelectedIndex
<
3
)
return
;
CheckBox m_checkBox;
for
(
int
i
=
0
;i
<
this
.DataList1.Items.Count;i
++
)
{
m_checkBox
=
this
.DataList1.Items[i].Controls[
1
]
as
CheckBox;
if
(m_checkBox.Checked)
{
Response.Write(
this
.DataList1.DataKeys[i].ToString()
+
"
:
"
+
m_checkBox.Text
+
"
<br>
"
);
}
//
Response.Write("i="+i.ToString()+":"+this.DataList1.Items[i].ToString()+"<br/>");
//
for(int j=0;j<this.DataList1.Items[i].Controls.Count;j++)
//
{
//
Response.Write("j="+j.ToString()+":"+ this.DataList1.Items[i].Controls[j].ToString()+"<br/>");
//
}
//
Response.Write("<br>==================================<br>");
}
}
================================
/\_/\
(=^o^=)
Wu.Country@侠缘
(~)@(~)
一辈子,用心做一件事!
--------------------------------
学而不思则罔,思而不学则怠!
================================
查看全文
相关阅读:
SpringBoot 集成Log4j、集成AOP
SpringBoot 集成JUnit
SpringBoot yml文件语法
SpringBoot 集成MyBatis、事务管理
SpringBoot 集成Spring JDBC
模板引擎简介
SpringBoot 解决“不支持发行版本xx”的问题
SpringBoot 全局异常处理
SpringBoot 静态资源的配置
SpringBoot的起步依赖
原文地址:https://www.cnblogs.com/WuCountry/p/327746.html
最新文章
Linux 文件、目录操作
CentOS、Ubuntu的下载安装
Linux 简介、目录结构
windows的启动、引导配置
消息中间件简介
SpringCloud Zuul 网关
SpringCloud Hystrix 监控仪表盘
SpringCloud Hystrix 容错保护
SpringCloud Feign 声明式服务调用
SpringCloud Ribbon 负载均衡
热门文章
分布式系统的CAP定理
IDEA 运行项目、模块的多个实例
SpringCloud Eureka 服务治理
Maven jar包冲突
Maven 父子工程的一些细节
Maven 多模块开发
Maven项目的目录问题
项目版本
SpringCloud 基础
微服务简介
Copyright © 2011-2022 走看看