转发器复选框oncheckchanged

时间:2016-03-14 09:23:42

标签: c# .net checkbox repeater

我有一个转发器控件,每个项目都有一个复选框。我无法弄清楚如何将OnCheckedChange事件用于此复选框。

 <asp:Repeater ID="rptImageList" runat="server" OnItemDataBound="rptImages_ItemDataBound" OnItemCommand="rptImages_ItemCommand">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Table runat="server" BorderStyle="None" HorizontalAlign="Left" Width="100%" GridLines="Both">
                <asp:TableRow>
                    <asp:TableCell Width="3%">
                        <asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="true" Checked='<%#((DataBinder.Eval(Container.DataItem,"IsSelected")!=null) && ((bool)DataBinder.Eval(Container.DataItem,"IsSelected")==true)) %>' OnCheckedChanged="cbSelect_CheckedChanged" />
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
    </asp:Repeater>

背后的代码

        protected void rptImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var cbSelect = e.Item.FindControl("cbSelect") as CheckBox;                
        }
    }

    protected void cbSelect_CheckedChanged(object sender, EventArgs e)
    {
    }

如何在cbSelect_CheckedChanged事件中使用复选框的状态? 我想在检查时更改它以更改它在数据库中分配的BIT字段!

1 个答案:

答案 0 :(得分:1)

您可以创建处理已检查更改和发件人的方法,因为CheckBox会将正确的checkbos发送到您的处理程序方法。

private void Checked_Handler(object sender, RoutedEventArgs e)
{
    HandleChecked(sender as CheckBox);
}

您还可以使用HandleChecked and HandleUnchecked Events

来自文档:

private void HandleCheck(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.Name == "cb1")
        text1.Text = "Two state CheckBox checked.";
    else
        text2.Text = "Three state CheckBox checked.";
}

private void HandleUnchecked(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.Name == "cb1")
        text1.Text = "Two state CheckBox unchecked.";
    else
        text2.Text = "Three state CheckBox unchecked.";
}

private void HandleThirdState(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    text2.Text = "Three state CheckBox indeterminate.";
}

和xaml:

<!-- two state CheckBox -->
<CheckBox x:Name="cb1" Content="Two State CheckBox" 
      Checked="HandleCheck" Unchecked="HandleUnchecked" Margin="5" />
<TextBlock x:Name="text1" Margin="5" />

<!-- three state CheckBox -->
<CheckBox x:Name="cb2" Content="Three State CheckBox" 
    IsThreeState="True" Checked="HandleCheck" 
    Indeterminate="HandleThirdState" Unchecked="HandleUnchecked" Margin="5" />
<TextBlock x:Name="text2" Margin="5" />