0

I have a datagrid (Asp.Net) 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

2 Answers 2

1

When do you add your custom column? If it is on load, then it is too late. Load it on init. I.e. following works with your code:

protected void Page_Init(object sender, EventArgs e)
{
    ItemTemplate myTemplate = new ItemTemplate();
    myTemplate.CheckedChanged += new EventHandler(myTemplate_CheckedChanged);

    TemplateField col = new TemplateField();
    col.ItemTemplate = myTemplate;
    col.ItemStyle.Wrap = false;

    grid.Columns.Add(col);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If your checkbox ID's are not being set the same way on every postback, then they can never be connected to the event handlers when it comes time to process the events. Where is your field "id" coming from?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.