在asp:repeater中,我使用asp:复选框,但OnCheckedChanged不会触发

时间:2012-07-21 08:05:17

标签: asp.net

以下是代码:

<asp:Repeater runat="server" ID="repeater1" OnItemCommand="checkChange">
    <ItemTemplate>
            <asp:Table runat="server" Width="80%">
            <asp:TableRow runat="server" HorizontalAlign="Center" Width="80%">
                <asp:TableCell Width="80%" HorizontalAlign="Left">
                    <asp:Button ID="Button1" runat="server" Text="删除" Visible="<%#administrator %>" />
                    <asp:CheckBox runat="server" ID="checkBox" Text='<%#DataBinder.Eval(Container.DataItem,"IID")%>' AutoPostBack="true" OnCheckedChanged="checkChange" EnableViewState="false"/>
                    <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"invText") %>'></asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            </asp:Table>
    </ItemTemplate>
    </asp:Repeater>

,功能是:

 protected void checkChange(object sender, EventArgs e)
    {
        Response.Write("it works well!");
    }

但是当我点击复选框时,没有任何事情发生......谢谢......

1 个答案:

答案 0 :(得分:1)

因为您为event handlerCheckBox提供了单Repeater

但当然因为OnItemCommandOnCheckedChanged事件都有相同的代理签名,即void target(object sender, EventArgs e)所以它不会引发任何类型的错误。

代码工作正常如下:

<asp:Repeater runat="server" ID="repeater1">
            <ItemTemplate>
                <asp:Table ID="Table1" runat="server" Width="80%">
                    <asp:TableRow ID="TableRow1" runat="server" HorizontalAlign="Center" Width="80%">
                        <asp:TableCell Width="80%" HorizontalAlign="Left">
                            <asp:Button ID="Button1" runat="server" Text="Button" />
                            <asp:CheckBox runat="server" ID="checkBox" Text="Click me if u dare." AutoPostBack="true"
                                OnCheckedChanged="checkChange" EnableViewState="false" />
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
            </ItemTemplate>
        </asp:Repeater>

后端

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            for (int i = 0; i < 5; i++)
                dt.Rows.Add(new object[] { "Name no.: " + i.ToString() });
            repeater1.DataSource = dt;
            repeater1.DataBind();
        }
    }

    protected void checkChange(object sender, EventArgs e)
    {
        Response.Write("Clicked called at :" + DateTime.Now.ToString("dd-MM-yy hh:mm:ss"));
    }