0

I need to generate a dynamic list of buttons, I already did, with an event handler attached to it.

However the event handler is not being executed.

private void GetOptions(EcoBonusRequest request)
        {
            var ecobonuswworkflow = WorkflowFactory.CreateEcobonusWorkflow();
            ecobonuswworkflow.SetCurrentStep(request.CurrentStatus);
            var currentoptions = ecobonuswworkflow.GetCurrentOptions();
            foreach(var option in currentoptions)
            {
                var btn = new Button() {Text = option.OptionName};
                btn.Click +=new EventHandler(btn_Click);
                Buttons.Controls.Add(btn);
            }

        }


        void btn_Click(object sender, EventArgs e)
        {
            var btn = (Button) sender;
            string command = btn.Text;
            EcoBonusRequest request = this.GetDBRequest(RequestBaseId.Value);
            EcoBonusRequestBL.AddWorkflowHistoryItem(request, command,CurrentUser, command);
        }
2
  • You need to recreate these button on every postback ( with the same ID as before and in page_load at the very latest ). Read(particularly part 3) TRULY Understanding Dynamic Controls Commented Jun 15, 2012 at 8:49
  • To be honest, i would just use a web databound control like repeater if possible. Commented Jun 15, 2012 at 8:55

1 Answer 1

2

The controls that you add dynamically in your page must be added in Page_init event, and they must have unique Ids. If you are adding textboxes or some other controls where user can input or change value, than on every post back when these controls are re-added they must have same IDs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.