日期:2014-05-18  浏览次数:20709 次

如何根据字符串选定CheckBoxList值
有字符串"1.0,2.0,2.1,”
如何选定

  <asp:CheckBoxList ID="cblRcdr" runat="server" RepeatDirection="Horizontal">
  <asp:ListItem Value="1.0">浏览</asp:ListItem>
  <asp:ListItem Value="1.1">增加</asp:ListItem>
  <asp:ListItem Value="1.2">修改</asp:ListItem>
  <asp:ListItem Value="1.3">删除</asp:ListItem>
  </asp:CheckBoxList>

  <asp:CheckBoxList ID="cblRcdrRecord" runat="server" RepeatDirection="Horizontal">
  <asp:ListItem Value="2.0">浏览</asp:ListItem>
  <asp:ListItem Value="2.1">增加</asp:ListItem>
  <asp:ListItem Value="2.2">修改</asp:ListItem>
  <asp:ListItem Value="2.3">删除</asp:ListItem>
  </asp:CheckBoxList>

------解决方案--------------------
ListItem x = cblRcdr.Items.FindByValue("1.2");
if(x!=null) x.Selected=true;
------解决方案--------------------
1.0,2.0,2.1,

Split循环即可

例子
HTML code

<%@ Page Language="C#" Debug="true" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    String xx = "1.0,2.0,2.1,";
    String[] xxx = xx.Split(',');
    for (int i = 0; i < xxx.Length; i++)
    {
      ListItem x = cblRcdr.Items.FindByValue(xxx[i]);
      if (x != null) x.Selected = true;

      x = cblRcdrRecord.Items.FindByValue(xxx[i]);
      if (x != null) x.Selected = true;
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
  <form id="form1" runat="server">
  <asp:CheckBoxList ID="cblRcdr" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Value="1.0">浏览</asp:ListItem>
    <asp:ListItem Value="1.1">增加</asp:ListItem>
    <asp:ListItem Value="1.2">修改</asp:ListItem>
    <asp:ListItem Value="1.3">删除</asp:ListItem>
  </asp:CheckBoxList>
  <asp:CheckBoxList ID="cblRcdrRecord" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Value="2.0">浏览</asp:ListItem>
    <asp:ListItem Value="2.1">增加</asp:ListItem>
    <asp:ListItem Value="2.2">修改</asp:ListItem>
    <asp:ListItem Value="2.3">删除</asp:ListItem>
  </asp:CheckBoxList>
  </form>
</body>
</html>