Custom control's click event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFyaw==?=

    Custom control's click event

    Hi, I have built a custom control button which inherits from WebControl and
    implements IPostBackEventH andler. 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(ob ject sender, EventArgs e)
    {

    ...

    BDTextButton bdtbSave = new BDTextButton();
    ...
    ...
    bdtbSave.ID = "btnSave";
    bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);



    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=?=

    #2
    RE: Custom control's click event

    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 IPostBackEventH andler. 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(ob ject sender, EventArgs e)
    {
    >
    ..
    >
    BDTextButton bdtbSave = new BDTextButton();
    ..
    ..
    bdtbSave.ID = "btnSave";
    bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);
    >
    >
    >
    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?
    >
    >

    Comment

    • =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=

      #3
      RE: Custom control's click event

      Howdy,

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

      protected void Page_Load(objec t sender, EventArgs e)
      {
      if (IsPostBack && RecreateDynamic Button)
      {
      CreateDynamicBu tton();
      }
      }

      private void CreateDynamicBu tton()
      {
      BDTextButton bdtbSave = new BDTextButton();
      ...
      ...
      bdtbSave.ID = "btnSave";
      bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);

      placeHodler.Con trols.Add(bdtbS ave);

      }

      protected void btnNew_Click(ob ject sender, EventArgs e)
      {
      ...
      CreateDynamicBu tton();

      }

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

      Hope this helps
      --
      Milosz


      "Mark" wrote:
      Hi, I have built a custom control button which inherits from WebControl and
      implements IPostBackEventH andler. 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(ob ject sender, EventArgs e)
      {
      >
      ..
      >
      BDTextButton bdtbSave = new BDTextButton();
      ..
      ..
      bdtbSave.ID = "btnSave";
      bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);
      >
      >
      >
      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?
      >
      >

      Comment

      • =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=

        #4
        RE: Custom control's click event

        Small bug: i forgot to set RecreateDynamic Button after first creation in new
        button click event handler, should be :

        protected void btnNew_Click(ob ject sender, EventArgs e)
        {
        ..
        CreateDynamicBu tton();
        RecreateDynamic Button = 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(objec t sender, EventArgs e)
        {
        if (IsPostBack && RecreateDynamic Button)
        {
        CreateDynamicBu tton();
        }
        }
        >
        private void CreateDynamicBu tton()
        {
        BDTextButton bdtbSave = new BDTextButton();
        ..
        ..
        bdtbSave.ID = "btnSave";
        bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);
        >
        placeHodler.Con trols.Add(bdtbS ave);
        >
        }
        >
        protected void btnNew_Click(ob ject sender, EventArgs e)
        {
        ..
        CreateDynamicBu tton();
        >
        }
        >
        private bool RecreateDynamic Button
        {
        get
        {
        object value = ViewState["RecreateDynami cButton"];
        return value == null ? false : (bool) value;
        }
        set
        {
        ViewState["RecreateDynami cButton"] = value;
        }
        }
        >
        Hope this helps
        --
        Milosz
        >
        >
        "Mark" wrote:
        >
        Hi, I have built a custom control button which inherits from WebControl and
        implements IPostBackEventH andler. 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(ob ject sender, EventArgs e)
        {

        ..

        BDTextButton bdtbSave = new BDTextButton();
        ..
        ..
        bdtbSave.ID = "btnSave";
        bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);



        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?

        Comment

        • =?Utf-8?B?TWFyaw==?=

          #5
          RE: Custom control's click event

          Thanks Milosz!




          "Milosz Skalecki [MCAD]" wrote:
          Small bug: i forgot to set RecreateDynamic Button after first creation in new
          button click event handler, should be :
          >
          protected void btnNew_Click(ob ject sender, EventArgs e)
          {
          ..
          CreateDynamicBu tton();
          RecreateDynamic Button = 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(objec t sender, EventArgs e)
          {
          if (IsPostBack && RecreateDynamic Button)
          {
          CreateDynamicBu tton();
          }
          }

          private void CreateDynamicBu tton()
          {
          BDTextButton bdtbSave = new BDTextButton();
          ..
          ..
          bdtbSave.ID = "btnSave";
          bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);

          placeHodler.Con trols.Add(bdtbS ave);

          }

          protected void btnNew_Click(ob ject sender, EventArgs e)
          {
          ..
          CreateDynamicBu tton();

          }

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

          Hope this helps
          --
          Milosz


          "Mark" wrote:
          Hi, I have built a custom control button which inherits from WebControl and
          implements IPostBackEventH andler. 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(ob ject sender, EventArgs e)
          {
          >
          ..
          >
          BDTextButton bdtbSave = new BDTextButton();
          ..
          ..
          bdtbSave.ID = "btnSave";
          bdtbSave.Click += new EventHandler(th is.btnSave_OnCl ick);
          >
          >
          >
          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?
          >
          >

          Comment

          Working...