Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamically Changing EventHandlers in Web page

Dynamically Changing EventHandlers in Web page

Scheduled Pinned Locked Moved ASP.NET
comalgorithmsdebugginghelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moon_stick
    wrote on last edited by
    #1

    I've already posted this on StackOverflow[^] but haven't seen much traffic yet... I've got a web form (set in a master page) and want to check to see if the page has any changes before exiting (either by success or redirect). If any changes have been made, a dialog is displayed and the user can click ok or cancel. I've created a panel to act as a dialog box (change z-layer, and apply shading and opacity to background) but my problem is in handling the click events from the buttons on the dialog. For a given exit scenario, I want to do something like:

    protected void btnWelcomePage_Click(object sender, ImageClickEventArgs e)
    {
    if (this.HasChanges())
    {
    lblDialogTitle.Text = "Confirm Exit";
    lblDialogMessage.Text = "Are you sure? Click OK to continue and lose any data entered";
    ClearEventHandlers(btnDialogOK);
    btnDialogOK_Click.Click += new EventHandler(btnDialogOK_Click);
    ClearEventHandlers(btDialogCancel);
    btnDialogCancel_Click += new EventHandler(btnDialogCancel_Click);
    pnlDialog.Visible = true;
    }
    else
    {
    Response.Redirect("~/WelcomePage.aspx");
    }
    }

    where the event handler for the buttons will do the appropriate work to hide the dialog and redirect if required. My problem is that the EventHandler doesn't appear to bind between postbacks - setting a breakpoint in the Click event handler doesn't do anything; the page does an empty postback. I assumed that the postback trashes the EventHandlers so I enabled ViewState on the buttons but to no avail. Can anyone suggest a model for handling this type of change? I've been searching through google but everything appears to relate to the dynamic creation of controls rather than the dynamic assignment of EventHandlers. Thanks in advance Dave

    It definitely isn't definatley

    P K 2 Replies Last reply
    0
    • M moon_stick

      I've already posted this on StackOverflow[^] but haven't seen much traffic yet... I've got a web form (set in a master page) and want to check to see if the page has any changes before exiting (either by success or redirect). If any changes have been made, a dialog is displayed and the user can click ok or cancel. I've created a panel to act as a dialog box (change z-layer, and apply shading and opacity to background) but my problem is in handling the click events from the buttons on the dialog. For a given exit scenario, I want to do something like:

      protected void btnWelcomePage_Click(object sender, ImageClickEventArgs e)
      {
      if (this.HasChanges())
      {
      lblDialogTitle.Text = "Confirm Exit";
      lblDialogMessage.Text = "Are you sure? Click OK to continue and lose any data entered";
      ClearEventHandlers(btnDialogOK);
      btnDialogOK_Click.Click += new EventHandler(btnDialogOK_Click);
      ClearEventHandlers(btDialogCancel);
      btnDialogCancel_Click += new EventHandler(btnDialogCancel_Click);
      pnlDialog.Visible = true;
      }
      else
      {
      Response.Redirect("~/WelcomePage.aspx");
      }
      }

      where the event handler for the buttons will do the appropriate work to hide the dialog and redirect if required. My problem is that the EventHandler doesn't appear to bind between postbacks - setting a breakpoint in the Click event handler doesn't do anything; the page does an empty postback. I assumed that the postback trashes the EventHandlers so I enabled ViewState on the buttons but to no avail. Can anyone suggest a model for handling this type of change? I've been searching through google but everything appears to relate to the dynamic creation of controls rather than the dynamic assignment of EventHandlers. Thanks in advance Dave

      It definitely isn't definatley

      P Offline
      P Offline
      Pranay Rana
      wrote on last edited by
      #2

      try to bind event with control when change are there in prerender event of the page hope this will work because code did by you is right it should work

      1 Reply Last reply
      0
      • M moon_stick

        I've already posted this on StackOverflow[^] but haven't seen much traffic yet... I've got a web form (set in a master page) and want to check to see if the page has any changes before exiting (either by success or redirect). If any changes have been made, a dialog is displayed and the user can click ok or cancel. I've created a panel to act as a dialog box (change z-layer, and apply shading and opacity to background) but my problem is in handling the click events from the buttons on the dialog. For a given exit scenario, I want to do something like:

        protected void btnWelcomePage_Click(object sender, ImageClickEventArgs e)
        {
        if (this.HasChanges())
        {
        lblDialogTitle.Text = "Confirm Exit";
        lblDialogMessage.Text = "Are you sure? Click OK to continue and lose any data entered";
        ClearEventHandlers(btnDialogOK);
        btnDialogOK_Click.Click += new EventHandler(btnDialogOK_Click);
        ClearEventHandlers(btDialogCancel);
        btnDialogCancel_Click += new EventHandler(btnDialogCancel_Click);
        pnlDialog.Visible = true;
        }
        else
        {
        Response.Redirect("~/WelcomePage.aspx");
        }
        }

        where the event handler for the buttons will do the appropriate work to hide the dialog and redirect if required. My problem is that the EventHandler doesn't appear to bind between postbacks - setting a breakpoint in the Click event handler doesn't do anything; the page does an empty postback. I assumed that the postback trashes the EventHandlers so I enabled ViewState on the buttons but to no avail. Can anyone suggest a model for handling this type of change? I've been searching through google but everything appears to relate to the dynamic creation of controls rather than the dynamic assignment of EventHandlers. Thanks in advance Dave

        It definitely isn't definatley

        K Offline
        K Offline
        kadaoui el mehdi
        wrote on last edited by
        #3

        to add controls (with eventhandlers) on pageweb you have to add it on page-init. try this:

        protected override void OnInit(EventArgs e)
        {
        base.OnInit(e);
        lblDialogTitle.Text = "Confirm Exit";
        lblDialogMessage.Text = "Are you sure? Click OK to continue and lose any data entered";
        ClearEventHandlers(btnDialogOK);
        btnDialogOK_Click.Click += new EventHandler(btnDialogOK_Click);
        ClearEventHandlers(btDialogCancel);
        btnDialogCancel_Click += new EventHandler(btnDialogCancel_Click);
        }

        protected void btnWelcomePage_Click(object sender, ImageClickEventArgs e)
        {
        if (this.HasChanges())
        {

            pnlDialog.Visible = true;
        }
        else
        {
            Response.Redirect("~/WelcomePage.aspx");
        }
        

        }

        good luck Mehdi

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups