views:

339

answers:

1

I have the following code:

   public partial class queryTerm : System.Web.UI.UserControl
    {
        private static readonly List<string> BooleanOperators = new List<string> { ".", "AND", "AND NOT", "OR", "OR NOT" };

        protected void BuildBoolPanel()
        {
            var parensOpen = _labelBoolean.Text;
            foreach (var @operator in BooleanOperators)
            {
                if (parensOpen == @operator)
                {
                    continue;
                }

                var linkButton = new LinkButton();
                linkButton.Text = @operator;
                linkButton.CommandArgument = @operator;
                linkButton.CommandName = "parensOpen";
                linkButton.Command += new CommandEventHandler(linkButton_Command);
                _popupMenuParensOpen.Controls.Add(linkButton);
                var literalLineBreak = new Literal();
                literalLineBreak.Text = "<BR/>";
                _popupMenuParensOpen.Controls.Add(literalLineBreak);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
                BuildBoolPanel();
        }

        void linkButton_Command(object sender, CommandEventArgs e)
        {
            _labelBoolean.Text = (string)e.CommandArgument;
            BuildBoolPanel();
        }
    }

I have a panel(it's _popupMenuParensOpen) that is shown with the hoverextender whenever the cursor finds itself over a specific label in my user control.

This panel has all the boolean operators and '.' meaning not set. I programatically add the boolean operators as a label in my panel, and I only add those that don't match what it is currently set to. For instance if my label is set to 'AND', when I hover over it, I display everything but 'AND'.


The problem is these never call linkButton_Command even though I instruct them to. Weirder yet, if I remove the 'if (!this.IsPostBack) in page load, it will call it.

My control is inside an updatePanel.

A: 

The issue is that you're dynamically adding the controls during the Page_Load event. When a postback occurs those controls aren't going to exist and will need to be created every time. This is why when you remove your if (!Page.IsPostBack), and the controls are rebuilt, it works.

Since you're building this as a user control, you might want to look in to overriding the CreateChildControls method.

CAbbott
Thanks for the answer!
Matt