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@侠缘
(~)@(~)
一辈子,用心做一件事!
--------------------------------
学而不思则罔,思而不学则怠!
================================
查看全文
相关阅读:
struts2 类型转化(typeConverter)
appfuse-maven-plugin(AMP)
多项式求和,素数判定 HDU2011.2012
A+B problems
黑马程序员----java基础笔记中(毕向东)
2015Web前端攻城之路
黑马程序员----java基础笔记上(毕向东)
黑马程序员----2015黑马之路-启程
乱弹琴20140421
读Thinking in java 4
原文地址:https://www.cnblogs.com/WuCountry/p/327746.html
最新文章
使用Debussy+ModelSim快速查看前仿真波形
allegro 有找到cmlib.dll,因此这个应用程序未能启动
设置vivado多线程编译
问题解决:import paddle.fluid出错:DLL load failed: 找不到指定的模块
2-机器学习入门-读书笔记
RxJava和EventBus的区别?
Android RecyclerView 使用完全解析 体验艺术般的控件
设计模式之抽象工程模式
设计模式之工厂方法模式
设计模式之简单工厂模式
热门文章
Android ART运行时无缝替换Dalvik虚拟机的过程分析
深入理解 hash 函数、HashMap、LinkedHashMap、TreeMap
Android 三大图片缓存原理、特性对比
Android动态加载技术 插件化技术
如何收集其Android应用性能数据的
hibernate 基本配置
jstl的基本操作
java 自定annotation
spring 扫描hibernate的映射文件
googlecode 上的dao-hibernate 使用
Copyright © 2011-2022 走看看