删除ItemTemplate上的eventhandler

时间:2012-10-25 08:04:54

标签: c# asp.net

我想在asp.net中删除ItemTemplate的事件处理程序。这是asp代码

 <ItemTemplate>
            <asp:RadioButton runat="server" ID="rdbAnswer"  GroupName="Group"  AutoPostBack="True" 
        OnCheckedChanged="rdbAnswer_CheckedChanged" />
        </ItemTemplate>

我想在后面的代码中删除checkedchanged事件。我怎样才能做到这一点 ?

1 个答案:

答案 0 :(得分:3)

使用-=删除事件处理程序。您可以在RowCreated中执行此操作:

protected void gridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        RadioButton rdbAnswer = (RadioButton)e.Row.FindControl("rdbAnswer");
        if(YourCondition)
        {
            // Remove event handler
            rdbAnswer.CheckedChanged -= new EventHandler(rdbAnswer_CheckedChanged);
            // maybe you also want to set rdbAnswer.AutoPostBack="false" to prevent the postback
        }
    }
}

请记住注册RowCreated事件处理程序:

<asp:GridView ID="gridView1" OnRowCreated="gridView1_RowCreated" runat="server">