倍耐利10号
坎坷的命运,独特的魅力,渲染了迷人的蓝色;坚强的意志,必胜的决心,铸就了王者的黑色。永不褪色的蓝黑——Internazional

导航

 
给添加Javascript脚本
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
        
<script type="text/javascript" language="javascript">
        function   CheckSelect() 
        
{
            
//alert("123");
            var tb=document.getElementById("CheckBoxList1");
            
for(var i=0;i<tb.rows.length;i++)
            
{
                var   chk     
=   tb.rows[i].firstChild.firstChild;
                
if(chk   !=   event.srcElement)  
                
{
                    chk.
checked=false;
                }

            }

        }

    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" onclick="CheckSelect()">
            
<asp:ListItem>First</asp:ListItem>
            
<asp:ListItem>Second</asp:ListItem>
            
<asp:ListItem>Three</asp:ListItem>
        
</asp:CheckBoxList>
    
</div>
    
</form>
</body>
</html>



如果不用JavaScript脚本,直接在后台代码实现也可以:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//先遍历CheckBoxList 取出只有1个选项时候的index
        int iCount=0;int iTemp=0;
        
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        
{
            
if (CheckBoxList1.Items[i].Selected)
            
{
                iCount
++;
                iTemp
=i;
            }

        }

        
if (iCount == 1)
        
{
            ViewState[
"oldSelect"= iTemp;
        }

    }

    
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
if (!CheckBox1.Checked)
        
{
            
int iCount = 0;
            
int iTemp = (Int32)ViewState["oldSelect"];
            
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            
{
                
if (CheckBoxList1.Items[i].Selected)
                
{
                    iCount
++;
                    
if (i != iTemp)
                    
{
                        ViewState[
"newSelect"= i;
                    }

                }

            }

            
if (iCount > 1)//点下第二个
            {
                
int iTempOld = (Int32)ViewState["oldSelect"];
                
int iTempNew = (Int32)ViewState["newSelect"];
                CheckBoxList1.Items[iTempOld].Selected 
= false;
                CheckBoxList1.Items[iTempNew].Selected 
= true;
                ViewState[
"oldSelect"= ViewState["newSelect"];
            }

        }


    }

}


posted on 2008-07-15 14:43  天高地厚  阅读(1315)  评论(0)    收藏  举报