将事件CheckedChanged动态添加到后页C#

时间:2017-05-31 13:54:54

标签: c#

我正在创建一个自定义CheckBox(CheckBoxColumn)

class CheckBoxColumn : ITemplate
{
    public event EventHandler CheckedChanged;

    public void InstantiateIn(System.Web.UI.Control container)
    {
        CheckBox cb = new CheckBox();
        cb.ID = "IDCheckBox";
        cb.AutoPostBack= true;
        container.Controls.Add(cb);
    }

    protected void OnCheckedChanged(object sender, EventArgs e)
    {
        if (this.CheckedChanged != null)
            this.CheckedChanged(this, e); 
    }
}

所以我可以在TemplateField中引入自定义复选框

            CheckBoxColumn cb = new CheckBoxColumn();
            cb.CheckedChanged += new EventHandler(ExecutarCheckBox_CheckedChanged);

            TemplateField checkBox = new TemplateField();
            checkBox.ItemTemplate = cb;
            gridviewexample.Columns.Add(checkBox);

但是从未到达OnCheckedChanged事件。 当我单击checkBox时,页面将重新加载,并且带有checkBox的列将消失。 有没有人知道如何才能让它发挥作用?

//代码

namespace Namespace1
{
    public partial class ClasseName: SuperClass
    {

        private void AddColumnsToGridView(DataTable dt)
        {

            CheckBoxColumn cb = new CheckBoxColumn();
            cb.CheckedChanged += new EventHandler(ExecutarCheckBox_CheckedChanged);

            TemplateField checkBox = new TemplateField();
            checkBox.ItemTemplate = cb;
            gridviewexample.Columns.Add(checkBox);

        }

    ...

        protected void ExecutarCheckBox_CheckedChanged(object sender, EventArgs e)
        {
             //code
        }

    }


    class CheckBoxColumn : ITemplate
    {
       public event EventHandler CheckedChanged;

       public void InstantiateIn(System.Web.UI.Control container)
       {
           CheckBox cb = new CheckBox();
           cb.ID = "IDCheckBox";
           cb.AutoPostBack= true;
           container.Controls.Add(cb);
        }

        protected void OnCheckedChanged(object sender, EventArgs e)
        {
            if (this.CheckedChanged != null)
                this.CheckedChanged(this, e); 
        }
    }

}

ASPX

<asp:GridView ID="gridviewexample" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="True" PagerType="DropDownList" PageSize="40" ShowFooter="true"

DataKeyNames="field1, field2, field3"

OnRowEditing="gridviewexample_RowEditing"
OnRowUpdating="gridviewexample_RowUpdating" 
OnPageIndexChanged="gridView_PageIndexChanged"
OnRowCancelingEdit="gridviewexample_RowCancelingEdit" 
OnSelectedIndexChanged="gridView_SelectedIndexChanged"
OnSorting="gridView_Sorting" 
OnRowDataBound="gridviewexample_RowDataBound"
OnPageIndexChanging="gridView_PageIndexChanging">                                    
<Columns>

       <asp:ButtonField ButtonType="Image" CommandName="Select" 
       ImageUrl="/images/edit.gif" >
            <ItemStyle HorizontalAlign="center" />
        </asp:ButtonField>                                        
        <asp:BoundField DataField="field1" ReadOnly="true" HeaderText="field1" Visible="true" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide" FooterStyle-CssClass= "Hide" />                        
        <asp:BoundField DataField="field2" ReadOnly="true" HeaderText="field2"/>
        <asp:BoundField DataField="field3" ReadOnly="true" HeaderText="field3" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide" FooterStyle-CssClass= "Hide" />                                     
</Columns>
    <EmptyDataTemplate>
        <span class="errorMsg" runat="server" id="lblNoRecords" visible='<%#(MadeSearch != null && MadeSearch.Value)?true:false%>'>
            </span>
    </EmptyDataTemplate>
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

我认为你必须将你的事件绑定到内部复选框的事件。 我无法测试,但我认为这至少会以正确的方式引导你:

class CheckBoxColumn : ITemplate
{
    private CheckBox _checkbox;
    private event EventHandler checkedChanged;
    public event EventHandler CheckedChanged
    {
        add
        {
            if (_checkbox != null)
            {
                checkedChanged += value;
                _checkbox.CheckedChanged += value;
            }
        }
        remove
        {
            if (_checkbox != null)
            {
                checkedChanged -= value;
                _checkbox.CheckedChanged -= value;
            }
        }
    }

    public CheckBoxColumn()
    {
        _checkbox = new CheckBox();
        _checkbox.ID = "IDCheckBox";
        _checkbox.AutoPostBack = true;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        if (_checkbox != null) container.Controls.Add(_checkbox);
    }

    protected void OnCheckedChanged(object sender, EventArgs e)
    {
        EventHandler handler = checkedChanged;
        if (handler != null) handler(this, e);
    }
}