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@侠缘
(~)@(~)
一辈子,用心做一件事!
--------------------------------
学而不思则罔,思而不学则怠!
================================
查看全文
相关阅读:
Docker入门(windows版),利用Docker创建一个Hello World的web项目
SpringBoot集成JWT实现token验证
Jedis的基本操作
Java动态代理详解
SpringBoot利用自定义注解实现通用的JWT校验方案
递归——汉诺塔问题(python实现)
Datatable删除行的Delete和Remove方法的区别
C# DEV使用心得
总结
安装插件时
原文地址:https://www.cnblogs.com/WuCountry/p/327746.html
最新文章
ZigZag Conversion
Centos下配置Jupyter notebook
Needleman-Wunsch算法Python代码实现
Anaconda使用简单教程及错误[errno 13]
tensorflow实现多层感知机
Django 在Python3.5 下报 没有模块MySQLdb
matlab 机器学习入门
pycharm2017 lanyu注册码失效(this license BIG3CLIK6F has been cancelled)解决方案
卷积神经网络识别Mnist图片
线性代数进阶
热门文章
python学习笔记---列表(四)
python学习笔记---math,random,operator(三)
python学习笔记---运算符(二)
python学习笔记---了解python(一)
VMware中安装Ubuntu
Mybatis分页查询total中的坑
Mybatis多表查询出现null字段
利用Docker快速部署Mysql
Mybatis实现多级菜单查询
docker 国内镜像源
Copyright © 2011-2022 走看看