2

这是代码:

<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!");
    }

但是当我单击复选框时,什么都没有发生...谢谢...

4

1 回答 1

1

因为您已经为event handlertheCheckBoxRepeater.

但当然因为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"));
    }
于 2012-07-21T08:15:37.000 回答