1

我有一个 asp.net 应用程序,我在用户控件中添加了中继器,并在中继器中添加了asp Checkbox

<asp:Repeater ID="Repeater1" runat="server" 
    onitemdatabound="Repeater1_ItemDataBound" 
    onitemcommand="Repeater1_ItemCommand" >
    <ItemTemplate>
        <table>
            <tr>
                <td>    
                     <asp:CheckBox runat="server" ID="chkSelect" AutoPostBack="true" Visible="false" OnCheckedChanged="chkSelect_CheckedChanged" />
                </td>

在 CodeBehind 文件中:

 protected void chkSelect_CheckedChanged(object sender, EventArgs e)
        {
            string s = "";
        }

但是单击复选框时该事件不会触发。

我还尝试在 Repeater 控件的 itemDataBound 事件触发时动态添加事件:

<asp:Repeater ID="Repeater1" runat="server" 
    onitemdatabound="Repeater1_ItemDataBound" 
    onitemcommand="Repeater1_ItemCommand" >
    <ItemTemplate>
        <table>
            <tr>
                <td>    
                     <asp:CheckBox runat="server" ID="chkSelect" AutoPostBack="true" Visible="false" />
                </td>

在后面的代码中:

   protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (((DataRowView)e.Item.DataItem).Row["DataType"].ToString().Trim().Equals("bool"))
            {
                CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
                chkSelect.CheckedChanged += new EventHandler(chkSelect_CheckedChanged);
                chkSelect.Visible = true;
            }

但仍然没有触发事件..

如何调用Repeater Control内部的CheckBox控件事件...

4

2 回答 2

0

只是颠倒逻辑。

默认情况下使用处理程序创建CheckBox可见的CheckedChanged

<asp:Repeater ID="Repeater1" runat="server" 
    onitemdatabound="Repeater1_ItemDataBound" 
    onitemcommand="Repeater1_ItemCommand" >
    <ItemTemplate>
        <table>
            <tr>
                <td>    
                     <asp:CheckBox runat="server" ID="chkSelect" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged" />
                </td>

ItemDataBound活动中,Visible=false为您不想显示的人设置:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (!((DataRowView)e.Item.DataItem).Row["DataType"].ToString().Trim().Equals("bool"))
    {
        CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
        chkSelect.Visible = false;
    }
}
于 2013-02-21T22:06:26.417 回答
0

ItemCreated如果您将事件连接到事件而不是事件中,您可能能够触发ItemDataBound事件:

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
   {
       CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
       if(chkSelect != null)
       {
            chkSelect.CheckedChanged += new EventHandler(chkSelect_CheckedChanged);
            chkSelect.Visible = true;
       }
   }

原因是ItemDataBound只会触发数据绑定。因此,当您回发时,您的Repeater 将从ViewState 重建,并ItemCreated为每一行执行,但ItemDataBound不是。

于 2013-02-21T17:29:02.113 回答