Custom control's click event

Discussion in 'ASP .Net' started by =?Utf-8?B?TWFyaw==?=, Mar 23, 2007.

  1. Hi, I have built a custom control button which inherits from WebControl and
    implements IPostBackEventHandler. The control also declares an event Click
    and provides a method OnClick which invokes the registered delegate. All is
    fine with my control until I try to add it to the page dynamically in a code
    behind event handler and wire-up the click event using the normal syntax such
    that;

    protected void btnNew_Click(object sender, EventArgs e)
    {

    ...

    BDTextButton bdtbSave = new BDTextButton();
    ...
    ...
    bdtbSave.ID = "btnSave";
    bdtbSave.Click += new EventHandler(this.btnSave_OnClick);



    When I do this the PostBack occurs but the event isn’t handled by the
    handler I provid, i.e. the event didn’t seem to register.
    After reading many posts I thought this may be because I was trying to
    register the event too late in the page’s lifecycle. But, as a test I
    dynamically added a Button webcontrol alongside my customcontrol and wired-up
    an event handler for this which worked!

    For further info my control is not a composite control it uses the Render
    event to write it’s content using the HtmlTextWriter.

    Why doesn’t my custom control’s event get registered when a webcontrol does?
    =?Utf-8?B?TWFyaw==?=, Mar 23, 2007
    #1
    1. Advertising

  2. I'm afraid you'll probably need to post the code for the control itself,
    rather than an example of how you are using it.
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Mark" wrote:

    > Hi, I have built a custom control button which inherits from WebControl and
    > implements IPostBackEventHandler. The control also declares an event Click
    > and provides a method OnClick which invokes the registered delegate. All is
    > fine with my control until I try to add it to the page dynamically in a code
    > behind event handler and wire-up the click event using the normal syntax such
    > that;
    >
    > protected void btnNew_Click(object sender, EventArgs e)
    > {
    >
    > ..
    >
    > BDTextButton bdtbSave = new BDTextButton();
    > ..
    > ..
    > bdtbSave.ID = "btnSave";
    > bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
    >
    >
    >
    > When I do this the PostBack occurs but the event isn’t handled by the
    > handler I provid, i.e. the event didn’t seem to register.
    > After reading many posts I thought this may be because I was trying to
    > register the event too late in the page’s lifecycle. But, as a test I
    > dynamically added a Button webcontrol alongside my customcontrol and wired-up
    > an event handler for this which worked!
    >
    > For further info my control is not a composite control it uses the Render
    > event to write it’s content using the HtmlTextWriter.
    >
    > Why doesn’t my custom control’s event get registered when a webcontrol does?
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Mar 23, 2007
    #2
    1. Advertising

  3. Howdy,

    Dynamically created controls are not persited bewteen postback. You have to
    recreate them go make postback event handler fire:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (IsPostBack && RecreateDynamicButton)
    {
    CreateDynamicButton();
    }
    }

    private void CreateDynamicButton()
    {
    BDTextButton bdtbSave = new BDTextButton();
    ...
    ...
    bdtbSave.ID = "btnSave";
    bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

    placeHodler.Controls.Add(bdtbSave);

    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
    ...
    CreateDynamicButton();

    }

    private bool RecreateDynamicButton
    {
    get
    {
    object value = ViewState["RecreateDynamicButton"];
    return value == null ? false : (bool) value;
    }
    set
    {
    ViewState["RecreateDynamicButton"] = value;
    }
    }

    Hope this helps
    --
    Milosz


    "Mark" wrote:

    > Hi, I have built a custom control button which inherits from WebControl and
    > implements IPostBackEventHandler. The control also declares an event Click
    > and provides a method OnClick which invokes the registered delegate. All is
    > fine with my control until I try to add it to the page dynamically in a code
    > behind event handler and wire-up the click event using the normal syntax such
    > that;
    >
    > protected void btnNew_Click(object sender, EventArgs e)
    > {
    >
    > ..
    >
    > BDTextButton bdtbSave = new BDTextButton();
    > ..
    > ..
    > bdtbSave.ID = "btnSave";
    > bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
    >
    >
    >
    > When I do this the PostBack occurs but the event isn’t handled by the
    > handler I provid, i.e. the event didn’t seem to register.
    > After reading many posts I thought this may be because I was trying to
    > register the event too late in the page’s lifecycle. But, as a test I
    > dynamically added a Button webcontrol alongside my customcontrol and wired-up
    > an event handler for this which worked!
    >
    > For further info my control is not a composite control it uses the Render
    > event to write it’s content using the HtmlTextWriter.
    >
    > Why doesn’t my custom control’s event get registered when a webcontrol does?
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 23, 2007
    #3
  4. Small bug: i forgot to set RecreateDynamicButton after first creation in new
    button click event handler, should be :

    protected void btnNew_Click(object sender, EventArgs e)
    {
    ..
    CreateDynamicButton();
    RecreateDynamicButton = true;
    }

    Have a nice evening

    --
    Milosz


    "Milosz Skalecki [MCAD]" wrote:

    > Howdy,
    >
    > Dynamically created controls are not persited bewteen postback. You have to
    > recreate them go make postback event handler fire:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (IsPostBack && RecreateDynamicButton)
    > {
    > CreateDynamicButton();
    > }
    > }
    >
    > private void CreateDynamicButton()
    > {
    > BDTextButton bdtbSave = new BDTextButton();
    > ..
    > ..
    > bdtbSave.ID = "btnSave";
    > bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
    >
    > placeHodler.Controls.Add(bdtbSave);
    >
    > }
    >
    > protected void btnNew_Click(object sender, EventArgs e)
    > {
    > ..
    > CreateDynamicButton();
    >
    > }
    >
    > private bool RecreateDynamicButton
    > {
    > get
    > {
    > object value = ViewState["RecreateDynamicButton"];
    > return value == null ? false : (bool) value;
    > }
    > set
    > {
    > ViewState["RecreateDynamicButton"] = value;
    > }
    > }
    >
    > Hope this helps
    > --
    > Milosz
    >
    >
    > "Mark" wrote:
    >
    > > Hi, I have built a custom control button which inherits from WebControl and
    > > implements IPostBackEventHandler. The control also declares an event Click
    > > and provides a method OnClick which invokes the registered delegate. All is
    > > fine with my control until I try to add it to the page dynamically in a code
    > > behind event handler and wire-up the click event using the normal syntax such
    > > that;
    > >
    > > protected void btnNew_Click(object sender, EventArgs e)
    > > {
    > >
    > > ..
    > >
    > > BDTextButton bdtbSave = new BDTextButton();
    > > ..
    > > ..
    > > bdtbSave.ID = "btnSave";
    > > bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
    > >
    > >
    > >
    > > When I do this the PostBack occurs but the event isn’t handled by the
    > > handler I provid, i.e. the event didn’t seem to register.
    > > After reading many posts I thought this may be because I was trying to
    > > register the event too late in the page’s lifecycle. But, as a test I
    > > dynamically added a Button webcontrol alongside my customcontrol and wired-up
    > > an event handler for this which worked!
    > >
    > > For further info my control is not a composite control it uses the Render
    > > event to write it’s content using the HtmlTextWriter.
    > >
    > > Why doesn’t my custom control’s event get registered when a webcontrol does?
    > >
    > >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 23, 2007
    #4
  5. Thanks Milosz!




    "Milosz Skalecki [MCAD]" wrote:

    > Small bug: i forgot to set RecreateDynamicButton after first creation in new
    > button click event handler, should be :
    >
    > protected void btnNew_Click(object sender, EventArgs e)
    > {
    > ..
    > CreateDynamicButton();
    > RecreateDynamicButton = true;
    > }
    >
    > Have a nice evening
    >
    > --
    > Milosz
    >
    >
    > "Milosz Skalecki [MCAD]" wrote:
    >
    > > Howdy,
    > >
    > > Dynamically created controls are not persited bewteen postback. You have to
    > > recreate them go make postback event handler fire:
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > if (IsPostBack && RecreateDynamicButton)
    > > {
    > > CreateDynamicButton();
    > > }
    > > }
    > >
    > > private void CreateDynamicButton()
    > > {
    > > BDTextButton bdtbSave = new BDTextButton();
    > > ..
    > > ..
    > > bdtbSave.ID = "btnSave";
    > > bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
    > >
    > > placeHodler.Controls.Add(bdtbSave);
    > >
    > > }
    > >
    > > protected void btnNew_Click(object sender, EventArgs e)
    > > {
    > > ..
    > > CreateDynamicButton();
    > >
    > > }
    > >
    > > private bool RecreateDynamicButton
    > > {
    > > get
    > > {
    > > object value = ViewState["RecreateDynamicButton"];
    > > return value == null ? false : (bool) value;
    > > }
    > > set
    > > {
    > > ViewState["RecreateDynamicButton"] = value;
    > > }
    > > }
    > >
    > > Hope this helps
    > > --
    > > Milosz
    > >
    > >
    > > "Mark" wrote:
    > >
    > > > Hi, I have built a custom control button which inherits from WebControl and
    > > > implements IPostBackEventHandler. The control also declares an event Click
    > > > and provides a method OnClick which invokes the registered delegate. All is
    > > > fine with my control until I try to add it to the page dynamically in a code
    > > > behind event handler and wire-up the click event using the normal syntax such
    > > > that;
    > > >
    > > > protected void btnNew_Click(object sender, EventArgs e)
    > > > {
    > > >
    > > > ..
    > > >
    > > > BDTextButton bdtbSave = new BDTextButton();
    > > > ..
    > > > ..
    > > > bdtbSave.ID = "btnSave";
    > > > bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
    > > >
    > > >
    > > >
    > > > When I do this the PostBack occurs but the event isn’t handled by the
    > > > handler I provid, i.e. the event didn’t seem to register.
    > > > After reading many posts I thought this may be because I was trying to
    > > > register the event too late in the page’s lifecycle. But, as a test I
    > > > dynamically added a Button webcontrol alongside my customcontrol and wired-up
    > > > an event handler for this which worked!
    > > >
    > > > For further info my control is not a composite control it uses the Render
    > > > event to write it’s content using the HtmlTextWriter.
    > > >
    > > > Why doesn’t my custom control’s event get registered when a webcontrol does?
    > > >
    > > >
    =?Utf-8?B?TWFyaw==?=, Mar 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    830
    Purvi T
    Oct 19, 2004
  2. Jonah Olsson
    Replies:
    1
    Views:
    571
    Scott Allen
    Apr 5, 2005
  3. Chu
    Replies:
    2
    Views:
    904
    Winista
    Apr 10, 2006
  4. Max2006
    Replies:
    2
    Views:
    1,565
    Walter Wang [MSFT]
    Sep 19, 2007
  5. James Tsao
    Replies:
    0
    Views:
    263
    James Tsao
    Oct 25, 2004
Loading...

Share This Page