locked
CheckedChanged event handler not called RRS feed

  • Question

  • User494151088 posted

    Hi everyone,

    I'm relatively new to ASP.NET and I got stuck trying to create my first simple server control. I'm having a hard time capturing a CheckedChanged event of a check box in a GridView when wiring everything via code. I tried to create handler when creating new instance CheckBoxColumn : ITemplate class and in the handler of RowDataBound, the code executes but the event handler of the checkbox it is never called. If anyone would have any feedback on the subject it would be of great help.

    Thank you,
    Radek


    // Code behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;


    public partial class _Default : System.Web.UI.Page
    {
    public class Person
    {
    private string _FirstName;
    public string FirstName
    {
    get { return _FirstName; }
    set { _FirstName = value; }
    }
    }


    void Page_Load(object sender, EventArgs e)
    {


    List<Person> lstPeople = new List<Person>();

    Person p = new Person();
    p.FirstName = "Bob";

    lstPeople.Add(p);

    sc1.GridView.DataSource = lstPeople;
    sc1.GridView.DataBind();

    }
    public void Register_Submit(object sender, EventArgs e)
    {
    // Works ok
    }

    }



    // Server control

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace TestServerControl
    {
    public class CheckBoxColumn : ITemplate
    {

    public void InstantiateIn(Control container)
    {
    CheckBox cb = new CheckBox();

    cb.AutoPostBack = true;
    cb.EnableViewState = true;
    cb.ID = "CBID";
    cb.DataBinding += new EventHandler(this.BindData);
    cb.CheckedChanged += new EventHandler(this.CheckedChanged_EventHandler);


    container.Controls.Add(cb);
    }

    private void CheckedChanged_EventHandler(object sender, EventArgs e)
    {
    // Never gets to it!
    }


    // Create a public method that will handle the
    // DataBinding event called in the InstantiateIn method.
    public void BindData(object sender, EventArgs e)
    {
    // Works ok
    }
    }

    [
    AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    DefaultEvent("Submit"),
    DefaultProperty("ButtonText"),
    ToolboxData("<{0}:Register runat=\"server\"> </{0}:Register>"),
    ]
    public class TestServerControl : CompositeControl
    {
    private Button submitButton;
    private GridView gridViewTest;
    private static readonly object EventSubmitKey = new object();


    [
    Bindable(true),
    Category("Appearance"),
    DefaultValue(""),
    Description("Main Grid View.")
    ]
    public GridView GridView
    {
    get
    {
    EnsureChildControls();
    return gridViewTest;
    }
    set
    {
    EnsureChildControls();
    gridViewTest = value;
    }
    }

    [
    Category("Action"),
    Description("Raised when the user clicks the button.")
    ]
    public event EventHandler Submit
    {
    add
    {
    Events.AddHandler(EventSubmitKey, value);
    }
    remove
    {
    Events.RemoveHandler(EventSubmitKey, value);
    }
    }

    // The method that raises the Submit event.
    protected virtual void OnSubmit(EventArgs e)
    {
    EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
    if (SubmitHandler != null)
    {
    SubmitHandler(this, e);
    }
    }

    private void _button_Click(object source, EventArgs e)
    {
    OnSubmit(EventArgs.Empty);
    }

    private void _gridView1_SelectedIndexChanged(object source, EventArgs e)
    {
    ; // Works ok
    }

    private void _gridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
    ; // Works ok
    }

    private void CheckedChanged_EventHandler(object sender, EventArgs e)
    {
    // Never gets to it!
    }

    private void _gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    CheckBox cb = (CheckBox)e.Row.FindControl("CBID");
    cb.CheckedChanged += new EventHandler(this.CheckedChanged_EventHandler);

    }

    }


    protected override void RecreateChildControls()
    {
    EnsureChildControls();
    }

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    }
    protected override void CreateChildControls()
    {
    Controls.Clear();

    submitButton = new Button();
    submitButton.ID = "button1";
    submitButton.Click += new EventHandler(_button_Click);


    gridViewTest = new GridView();
    gridViewTest.ID = "GridView1";
    gridViewTest.AutoGenerateSelectButton = true;
    gridViewTest.AutoGenerateColumns = false;
    gridViewTest.Attributes.Add("runat", "server");

    BoundField firstName = new BoundField();
    firstName.HeaderText = "First Name";
    firstName.DataField = "FirstName";
    gridViewTest.Columns.Add(firstName);

    gridViewTest.SelectedIndexChanged += new EventHandler(_gridView1_SelectedIndexChanged);
    gridViewTest.RowCommand += new GridViewCommandEventHandler(_gridView1_RowCommand);
    gridViewTest.RowDataBound += new GridViewRowEventHandler(_gridView1_RowDataBound);


    TemplateField checkBoxTemplateField = new TemplateField();
    checkBoxTemplateField.ItemTemplate = new CheckBoxColumn();
    gridViewTest.Columns.Add(checkBoxTemplateField);


    this.Controls.Add(submitButton);
    this.Controls.Add(gridViewTest);

    }


    protected override void Render(HtmlTextWriter writer)
    {
    writer.Write("Hello Server Control");
    gridViewTest.RenderControl(writer);
    submitButton.RenderControl(writer);

    }
    }
    }

    Monday, May 16, 2011 10:24 PM

Answers

  • User3866881 posted

    Hi:)
    Not an expert in this issue, but considering no one is replying to you, I'd like to say——

    I guess——

    public void InstantiateIn(Control container)
    {
    CheckBox cb = new CheckBox();

    cb.AutoPostBack = true;
    cb.EnableViewState = true;
    cb.ID = "CBID";
    ……

    }

    Everytime when your control is reloaded again and again, a new CheckBox will be created again and again. So everytime you always get the new Checkbox, and the event cannot be fired.

    My suggestion is that you should try to use something to remember the newly-created control.

    Thx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 18, 2011 9:59 PM

All replies

  • User3866881 posted

    Hi:)
    Not an expert in this issue, but considering no one is replying to you, I'd like to say——

    I guess——

    public void InstantiateIn(Control container)
    {
    CheckBox cb = new CheckBox();

    cb.AutoPostBack = true;
    cb.EnableViewState = true;
    cb.ID = "CBID";
    ……

    }

    Everytime when your control is reloaded again and again, a new CheckBox will be created again and again. So everytime you always get the new Checkbox, and the event cannot be fired.

    My suggestion is that you should try to use something to remember the newly-created control.

    Thx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 18, 2011 9:59 PM
  • User494151088 posted

    Hi :)

    Yes, I managed to debug it in the end. I was binding the data from the parent page before CreateChildControls was called when the new grid was being overwriten including all the controls, all I had to do is to bind at the end of CreateChildControls to fix the problem.  Not claiming to be an expert quite correct, thank you :)

    Wednesday, May 25, 2011 11:46 PM