C# – Problem in event generation of dynamically generated CheckBox

asp.netc

I have a datagrid with dynamically generated checkbox column..I am not able to
generate the checkedChanged event for the checkbox..

Here is my code:

public class ItemTemplate : ITemplate
{
    //Instantiates the checkbox
    void ITemplate.InstantiateIn(Control container)
    {
        CheckBox box = new CheckBox();            
        box.CheckedChanged += new EventHandler(this.OnCheckChanged);
        box.AutoPostBack = true;
        box.EnableViewState = true;
        box.Text = text;
        box.ID = id;
        container.Controls.Add(box);
    }

    public event EventHandler CheckedChanged;

    private void OnCheckChanged(object sender, EventArgs e)
    {
        if (CheckedChanged != null)
        {
            CheckedChanged(sender, e);
        }
    }
}

and Here is the event

private void OnCheckChanged(object sender, EventArgs e)
{

}

Thanks In advance

Best Answer

Typically we've used the "CommandName" property on the control. This will pass through to the RowCommand event of the GridView. You can then inspect the value of CommandName and act accordingly.

Related Topic