我想用javascript根据checkboxlist的选定项中是否有指定项(如“天气”),来控制一个dropdownlist控件是否显示,不能在后台写,因为不想刷新页面。checkboxlist的datasource是从数据库绑定的,因为数量是动态的,所以不能用checkbox 
有没有什么好的实现办法?还请大家多帮忙!

解决方案 »

  1.   

    asp.net2.0还是以前的?
    asp.net2.0可以直接使用Attributes.Add("onclick","jsMethod();return false");实现
      

  2.   

    ajax,想不刷新用前台控件去改变后台控件,用传统办法行不通
      

  3.   

    <%@ Page Language="C#" %><!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 )
      {
        if (!Page.IsPostBack)
        { 
          string[] a = {"A","B"};
          CheckBoxList1.DataSource = a;
          CheckBoxList1.DataBind();
          for (int i = 0 ; i < CheckBoxList1.Items.Count ; i++)
          {
            if (CheckBoxList1.Items[i].Text == "A")
            {
              CheckBoxList1.Items[i].Attributes.Add("onclick", "document.getElementById('" + DropDownList1.ClientID + "').style.display='block'");
            }
            else
            {
              CheckBoxList1.Items[i].Attributes.Add("onclick", "document.getElementById('" + DropDownList1.ClientID + "').style.display='none'");
            }
            
          }
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <asp:CheckBoxList ID="CheckBoxList1" runat="server">
          </asp:CheckBoxList></div>
          <asp:DropDownList ID="DropDownList1" runat="server">
          </asp:DropDownList>
        </form>
    </body>
    </html>
      

  4.   

    <%@ Page Language="C#" %><!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 )
      {
        if (!Page.IsPostBack)
        {
          string[] a = { "天气", "B", "C"};
          CheckBoxList1.DataSource = a;
          CheckBoxList1.DataBind();
          for (int i = 0 ; i < CheckBoxList1.Items.Count ; i++)
          {
            if (CheckBoxList1.Items[i].Text == "天气")
            {
              CheckBoxList1.Items[i].Attributes.Add("onclick", "document.getElementById('" + DropDownList1.ClientID + "').style.display='none'");
            }
            else
            {
              CheckBoxList1.Items[i].Attributes.Add("onclick", "document.getElementById('" + DropDownList1.ClientID + "').style.display=''");
            }
            
          }
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="false">
          </asp:CheckBoxList></div>
          <asp:DropDownList ID="DropDownList1" runat="server">
          </asp:DropDownList>
        </form>
    </body>
    </html>
      

  5.   

    或者
    <%@ Page Language="C#" %><!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 )
      {
        if (!Page.IsPostBack)
        {
          string[] a = { "天气", "B", "C"};
          CheckBoxList1.DataSource = a;
          CheckBoxList1.DataBind();
          for (int i = 0 ; i < CheckBoxList1.Items.Count ; i++)
          {
            if (CheckBoxList1.Items[i].Text == "天气")
            {
              CheckBoxList1.Items[i].Attributes.Add("onclick", "if(this.checked) {document.getElementById('" + DropDownList1.ClientID + "').style.display='none'}else{document.getElementById('" + DropDownList1.ClientID + "').style.display=''}");
            }      
          }
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="false">
          </asp:CheckBoxList></div>
          <asp:DropDownList ID="DropDownList1" runat="server">
          </asp:DropDownList>
        </form>
    </body>
    </html>
    也可以这样<%@ Page Language="C#" %><!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 )
      {
        if (!Page.IsPostBack)
        {
          string[] a = { "天气", "B", "C" };
          CheckBoxList1.DataSource = a;
          CheckBoxList1.DataBind();
          for (int i = 0 ; i < CheckBoxList1.Items.Count ; i++)
          {
            CheckBoxList1.Items[i].Attributes.Add("onclick", "ClickMe(this)");
          }
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>  <script type="text/javascript">
        function ClickMe(o)
        {
          var boxid = "<%=CheckBoxList1.ClientID %>"
          var boxs = document.getElementsByTagName("INPUT")
          for(i = 0;i<boxs.length;i++)
          {
           if(boxs[i].id.substr(0,boxid.length) == boxid)
           {
            var chname = boxs[i].nextSibling.innerHTML
      
            if(chname == "天气" && boxs[i].checked)
            {
              document.getElementById('<%=DropDownList1.ClientID%>').style.display='none';
              break;
            }
            else
            {
            document.getElementById('<%=DropDownList1.ClientID%>').style.display='';
            }
           }
          }
        }
            
      </script></head>
    <body>
      <form id="form1" runat="server">
        <div>
          <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="false">
          </asp:CheckBoxList></div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
      </form>
    </body>
    </html>随你的需要而定
      

  6.   

    前台如果用了protected void Page_Load( object sender, EventArgs e ),后台的就被屏蔽了,能把前台的换个名字吗?也让它在初始化页面时执行?
      

  7.   

    另外因为页面继承了master页,所以没有body,没法用onload事件
      

  8.   

    OnClick该成OnCheckedChanged事件试试
      

  9.   

    OnClick该成OnCheckedChanged事件试试
    ============================
    试过,不行
      

  10.   

    uping............................................................................................小女帮你顶一次
      

  11.   

    另外因为页面继承了master页,所以没有body,没法用onload事件??
    总的页面是有的window.onload=function(){alert("ok")}
    服务器端的Page_Load更没有问题