Displaying a Dialog Box On-the-Fly

Last post 11-01-2009 9:41 AM by fayaz_3e. 5 replies.

Sort Posts:

  • Displaying a Dialog Box On-the-Fly

    10-30-2009, 5:42 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    I want to create common code that will display a dialog box, prompting the user for feedback.  Here's what I've created so far:

      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
          <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </ContentTemplate>
      </asp:UpdatePanel>

      public static void ShowDialog(string msg)
        {
          Page page = HttpContext.Current.CurrentHandler as Page;

          Panel panel = new Panel();
          panel.ID = "panelError";
          panel.CssClass = "errorDialog";

          Label labelMsg = new Label();
          panel.Controls.Add(labelMsg);

          msg += "<br/><br/>Please describe what you were doing when this occurred:<br/>";
          labelMsg.Text = msg;

          TextBox textBox = new TextBox();
          textBox.ID = "textBoxFeedback";
          textBox.Style.Add("width", "95%");
          textBox.Style.Add("background-color", "#ffff40");
          textBox.Height = 40;
          panel.Controls.Add(textBox);

          Button button = new Button();
          button.Text = "OK";
          button.Width = 50;
          button.Style.Add("margin-top", "20px");
          panel.Controls.Add(button);

          page.Form.Controls.Add(panel);

          Website.Common.SetFocus(textBox);
        }

    It's not displaying though and I believe it's because that I somehow need to force a partial postback.  How would I do that?

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Displaying a Dialog Box On-the-Fly

    10-31-2009, 7:23 AM
    Answer
    • Contributor
      4,151 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 10:15 AM
    • Hyderabad
    • Posts 869

    Dear,

    Thats interesting. But the logic you have is a bit tedious. You are doing that in code behind. Why dont you do that in javscript?? You can do the same in script.

    Any way, since you are using update panel, adding new controls to page controls is incorrect. UpdatePanel will update its content only, not the whole page. So Add a place holder to ContentTemplate then add your controls to that place holder...

    I ve written a sample. Used a place holder "phFeedback", and adding to it. Refer this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public static class FeedBack
    {
        public static void ShowDialog(string msg)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
    
            Panel panel = new Panel();
            panel.ID = "panelError";
            panel.CssClass = "errorDialog";
    
            Label labelMsg = new Label();
            panel.Controls.Add(labelMsg);
    
            msg += "<br/><br/>Please describe what you were doing when this occurred:<br/>";
            labelMsg.Text = msg;
    
            TextBox textBox = new TextBox();
            textBox.ID = "textBoxFeedback";
            textBox.Style.Add("width", "95%");
            textBox.Style.Add("background-color", "#ffff40");
            textBox.Height = 40;
            panel.Controls.Add(textBox);
    
            Button button = new Button();
            button.Text = "OK";
            button.Width = 50;
            button.Style.Add("margin-top", "20px");
            panel.Controls.Add(button);
            var ph = page.GetAllControls().OfType<PlaceHolder>().Where(a => a.ID == "phFeedBack").ToList();
            if(ph.Count!=0)
            ((PlaceHolder)ph[0]).Controls.Add(panel);
          
        }
    
        public static IEnumerable<Control> GetAllControls(this Control parent)
        {
            foreach (Control control in parent.Controls)
            {
                yield return control;
                foreach (Control descendant in control.GetAllControls())
                {
                    yield return descendant;
                }
            }
        }
    }
    

    I used LINQ. If LINQ is not applicable then write a for loop and get all controls. Dont forget to add place holder to ContentTemplate of UpdataPanel... And I recommend you to use JavaScript if possible Smile


    Fayaz
  • Re: Displaying a Dialog Box On-the-Fly

    10-31-2009, 3:01 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    Fayaz,

    In your example code, what is causing the Partial Postback to force the controls to appear?  I don't see it anywhere.

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Displaying a Dialog Box On-the-Fly

    10-31-2009, 11:26 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    Fayez,

    Wow, it worked!  I didn't do it exactly as you did.  Instead, I just added a PlaceHolder control and then issued this command:  placeHolder.Controls.Add(panel);

    And it worked!!!

    Now I want to see if I can dynamically add a PlaceHolder on-the-fly.

    Robert


    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Displaying a Dialog Box On-the-Fly

    11-01-2009, 9:31 AM
    • Contributor
      4,151 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 10:15 AM
    • Hyderabad
    • Posts 869

    rmdw:

    In your example code, what is causing the Partial Postback to force the controls to appear?  I don't see it anywhere.

    Robert

    Partial post back occured because the button is inside UpdatePanel. Hence it triggers Asynchrnous PostBack. Update panel will update only its content and there is one more concept like UpdateMode for conditional update or always update. So we took a container(PlaceHolder or Panel) and placed in ContentTemplate of UpdatePanel and adding controls to it. Since the container is inside updatepanel, newly added controls will be displayed.

    Fayaz
  • Re: Displaying a Dialog Box On-the-Fly

    11-01-2009, 9:41 AM
    • Contributor
      4,151 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 10:15 AM
    • Hyderabad
    • Posts 869

    rmdw:
    I didn't do it exactly as you did.  Instead, I just added a PlaceHolder control and then issued this command:  placeHolder.Controls.Add(panel);

    The code I have provided is a generic approach. Since you said a common code, I assumed you may want to execute it from any page irrespective of whether you have master page or not.

    So what am doing is took the same page object and getting the place holder and adding the controls to it. This will work for any page but you should have place holder with same id as in the code.

    rmdw:
    Now I want to see if I can dynamically add a PlaceHolder on-the-fly.

    You can add place holder also. But all I want to say is you should add to a container which is inside update panel or directly add to update panel content template. Instead of getting place holder you can get update panel and can add controls to content template...

    Fayaz
Page 1 of 1 (6 items)