DataList中的Checkbox取值问题
2012-09-01 16:27
246 查看
先看页面代码
<asp:DataList id="DataList1" runat="server" Width="100%" RepeatColumns="4" Font-Size="10pt">
<ItemTemplate>
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" bgColor="gainsboro" border="0">
<TR>
<TD align="center">
<asp:Label id=lblOName runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_name")%>'>
</asp:Label>
<asp:Label id=lblID runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_id")%>' Visible="False">
</asp:Label></TD>
</TR>
<TR>
<TD align="center">
<asp:CheckBoxList id="cbSelect" runat="server" Font-Size="10pt" RepeatColumns="2" Width="100%" BackColor="#FFE0C0"
RepeatDirection="Horizontal" DataTextField="nmenu_tname" DataValueField="nmenu_tid"></asp:CheckBoxList></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:DataList>
后台绑定CheckBox:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
if(Session["UID"]!=null&&Session["UID"].ToString()!="")
{
BindList();
BindCheckBox();
}
else
{
Server.Transfer("error.aspx");
}
}
}
private void BindList()
{
string sql = "select * from NewsOne";
DataList1.DataSource = ort.ExecuteToTable(sql);
DataList1.DataBind();
}
private void BindCheckBox()
{
foreach(DataListItem dli in DataList1.Items)
{
CheckBoxList cbl = (CheckBoxList)dli.FindControl("cbSelect");
Label lbl = (Label)dli.FindControl("lblID");
string sql = "select * from NewsTwo where nmenu_oid="+lbl.Text;
cbl.DataSource = ort.ExecuteToTable(sql);
cbl.DataBind();
}
}
CheckBox取值:
string list = "";
foreach(DataListItem dli in DataList1.Items)
{
CheckBoxList cbSel = (CheckBoxList)dli.FindControl("cbSelect");
for(int i=0;i<cbSel.Items.Count;i++)
{
if(cbSel.Items[i].Selected == true)
{
list += cbSel.Items[i].Value + ",";
}
}
}
list = list.Trim(",".ToCharArray());
有的时候会出现取不到值的情况,我的看法是缺少if(!IsPostBack)。
<asp:DataList id="DataList1" runat="server" Width="100%" RepeatColumns="4" Font-Size="10pt">
<ItemTemplate>
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" bgColor="gainsboro" border="0">
<TR>
<TD align="center">
<asp:Label id=lblOName runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_name")%>'>
</asp:Label>
<asp:Label id=lblID runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_id")%>' Visible="False">
</asp:Label></TD>
</TR>
<TR>
<TD align="center">
<asp:CheckBoxList id="cbSelect" runat="server" Font-Size="10pt" RepeatColumns="2" Width="100%" BackColor="#FFE0C0"
RepeatDirection="Horizontal" DataTextField="nmenu_tname" DataValueField="nmenu_tid"></asp:CheckBoxList></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:DataList>
后台绑定CheckBox:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
if(Session["UID"]!=null&&Session["UID"].ToString()!="")
{
BindList();
BindCheckBox();
}
else
{
Server.Transfer("error.aspx");
}
}
}
private void BindList()
{
string sql = "select * from NewsOne";
DataList1.DataSource = ort.ExecuteToTable(sql);
DataList1.DataBind();
}
private void BindCheckBox()
{
foreach(DataListItem dli in DataList1.Items)
{
CheckBoxList cbl = (CheckBoxList)dli.FindControl("cbSelect");
Label lbl = (Label)dli.FindControl("lblID");
string sql = "select * from NewsTwo where nmenu_oid="+lbl.Text;
cbl.DataSource = ort.ExecuteToTable(sql);
cbl.DataBind();
}
}
CheckBox取值:
string list = "";
foreach(DataListItem dli in DataList1.Items)
{
CheckBoxList cbSel = (CheckBoxList)dli.FindControl("cbSelect");
for(int i=0;i<cbSel.Items.Count;i++)
{
if(cbSel.Items[i].Selected == true)
{
list += cbSel.Items[i].Value + ",";
}
}
}
list = list.Trim(",".ToCharArray());
有的时候会出现取不到值的情况,我的看法是缺少if(!IsPostBack)。
相关文章推荐
- jquery对checkbox的取值问题
- JQuery实现Checkbox去选时的取值问题IE6和IE8
- 关于checkbox的取值问题
- <s:checkbox> fieldValue取值问题
- 关于radio checkbox select的值的选中和取值的问题
- IE表单在DOM结构被修改后,checkbox和radio取值问题
- datalist中使用checkbox的问题
- JavaScript 判断checkbox取值问题
- Extjs—checkbox的取值以及修改时已选项的回显问题
- 分享关于Extjs2.0中RadioGroup和CheckboxGroup控件的取值和赋值问题
- 遇到的一个html控件checkbox全选反选问题后台取值问题
- 关于在DataList中取值的问题--在有页眉时的判断
- Android ListView中CheckBox取值问题
- 修复checkbox的状态切换和动态取值的问题,主要是学会jquery选择或者不选中checkbox
- ExtJS3.4.0 radioGroup,checkboxgroup 在 getForm().getValues()取值问题的解决方案
- 对于checkBox取值问题解决
- 关于checkboxlist前台取值问题
- winform 下 从DataGridView 的 DataGridViewCheckBoxCell 列取值和保存问题
- winform 下 从DataGridView 的 DataGridViewCheckBoxCell 列取值和保存问题
- checkbox取值问题