如何在ListView中一次只检查一个复选框?

时间:2012-02-06 23:37:39

标签: asp.net vb.net listview checkbox

我有一个ListView,每行包含3个复选框。我想设置它,以便一次只能选择一个复选框。这是我目前的CodeBehind ...

Public Class MyClass

    Dim Checkbox1 As Checkbox
    Dim Checkbox2 As Checkbox
    Dim Checkbox3 As Checkbox

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        InitializeDisplay()

        If Not IsPostBack Then
           UpdateDisplay()
        End If

    End Sub

    Protected Sub UpdateDisplay()

        Dim Table As DataTable
        Table = GetDataTable()

        ListView.DataSource = Table
        ListView.DataBind()  

    End Sub

    Private Sub ListView_ItemDataBound( sender, e ) Handles ListView.ItemDataBound

        Dim item As ListViewItem = e.Item

        Checkbox1 = item.FindControl("Checkbox1")
        Checkbox2 = item.FindControl("Checkbox2")
        Checkbox3 = item.FindControl("Checkbox3")

    End Sub

    Public Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim Checkbox2 As CheckBox = TryCast(sender, CheckBox)
        Dim Checkbox3 As CheckBox = TryCast(sender, CheckBox)

        Checkbox2.Checked = False
        Checkbox3.Checked = False        

    End Sub

    Public Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim Checkbox1 As CheckBox = TryCast(sender, CheckBox)
        Dim Checkbox3 As CheckBox = TryCast(sender, CheckBox)

        Checkbox1.Checked = False
        Checkbox3.Checked = False        

    End Sub

    Public Sub CheckBox3_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim Checkbox1 As CheckBox = TryCast(sender, CheckBox)
        Dim Checkbox2 As CheckBox = TryCast(sender, CheckBox)

        Checkbox1.Checked = False
        Checkbox2.Checked = False        

    End Sub

End Class

ListView中的实际WebForm ItemTemplate

<ItemTemplate>
<asp:Checkbox ID="CheckBox1" OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" />
<asp:Checkbox ID="CheckBox2" OnCheckedChanged="CheckBox2_CheckedChanged" runat="server" />
<asp:Checkbox ID="CheckBox3" OnCheckedChanged="CheckBox3_CheckedChanged" runat="server" />
</ItemTemplate>

如果我需要包含其他任何内容,请与我们联系。现在当我点击一个复选框并选择另一个复选框时,两个都被选中而不是只选择新的...任何想法?谢谢!

3 个答案:

答案 0 :(得分:1)

您需要在每个复选框(Doco here)上设置AutoPostBack =“true”。否则,在以其他方式触发回发之前,您的服务器端代码将永远不会运行。

我个人更喜欢使用jQuery来做,以避免往返服务器。为此,尝试类似

的内容
$(document).ready(function(){
   $("input[type=checkbox]").click(function(eventData){
        var checked = $(eventData.currentTarget).prop("checked");
        if(checked === "checked"){
             $("input[type=checkbox]").prop("checked", false);//uncheck everything.
             $(eventData.currentTarget).prop("checked", "checked");//recheck this one. 
        }
   });
});

答案 1 :(得分:0)

我已经完成了一个,我相信你可以弄清楚剩下的需要什么。这也可以通过为所有3个复选框创建一个处理程序来加以控​​制

我对.net不是很好,但我知道它可能。

.....

Public Sub CheckBox1_CheckedChanged(ByVal sender As Object,ByVal e As EventArgs)

Dim Checkbox2 As CheckBox = TryCast(sender,CheckBox) Dim Checkbox3 As CheckBox = TryCast(sender,CheckBox)

Checkbox2.Checked = Not sender.checked Checkbox3.Checked = Not sender.checked

End Sub

虽然如上所述,单选按钮通常更适合此应用程序。

答案 2 :(得分:0)

$(document).ready(function(){

   $("input[type=checkbox]").click(function(eventData){

        var checked = $(eventData.currentTarget).prop("checked");

        if(checked){

             $("input[type=checkbox]").prop("checked", false);//uncheck everything.
             $(eventData.currentTarget).prop("checked", "checked");//recheck this one. 
        }
   });

});