简体   繁体   中英

OnClick event of my LinkButton is never triggered

I want to generate dynamic dropdownlists and a linkbutton based on a selectedItem from a dropdownlist. Here the scenario: 1. User selects a familyname from "ddlFamily" 2. Dynamic ddls and "submitAnswers" linkbutton will be created. These ddls will include some specific questions related to the selected family. Users will select answers from the dynamically created ddls. 3. User will click the linkbutton and save the answers.

My problem is, when I click the linkbutton, the buttonclick event "SubmitAnswers_Click" is never triggered. I could not find and fix why. My code is on the below:

protected void ddlFamily_SelectedIndexChanged(object sender, EventArgs e)
            {
                questions = GetQuestions();
                int counter = 0;
                while (counter < questions.Count)
                {
                    DropDownList ddl = new DropDownList();
                    ddl.ID = "dynamicDDL" + counter.ToString();
                    ddl.Items.Add(questions[counter]);
                    String q2 = "SELECT ANSWERID, answer FROM Answer WHERE questionID="+questionIDs[counter];
                    ReturnResultViaQueryToDB(answers, answerIDs, q2);
                    for (int i = 0; i < answers.Count; i++)
                    {
                        ddl.Items.Add(answers[i]);
                    }
                    pnlFamily.Controls.Add(ddl);
                    counter++;
                    answers.Clear();
                    answerIDs.Clear();
                    howManyDDLCreated++;
                }
                if (howManyDDLCreated > 0)
                {
                    LinkButton lb = new LinkButton();
                    lb.ID = "bnSubmitAnswers";
                    lb.Attributes.Add("AutoPostBack", "true");
                    lb.ForeColor = System.Drawing.Color.Yellow;
                    lb.Click += new EventHandler(submitAnswers_Click);
                    lb.Text = "Submit Answers";
                    pnlProvince.Controls.Add(lb);
                }
            }
        }
        protected void submitAnswers_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < howManyDDLCreated; i++)
            {
                var currentDynamicDDL = (DropDownList)Page.FindControl("DynamicDDL" + i);
                String s = currentDynamicDDL.Text;
//save answers here
            }
        }

Thank you in advance for all helps. I appreciate.

By the way, I checked the asked questions and found some similar ones. However, I could not find an appropriate answer, unfortunately. Very sorry if any and if I miss.

Sincerely

This is down to the page life cycle of web forms. If you add controls to the aspx markup then the code behind will handle creation of these controls on post back. If you create dynamic controls then you need to recreate them yourself at the appropriate stage of the life cycle. The Page_init event is the safest place and should allow the view state to populate the state and fire the event. You will need to reattach the event handler. Microsoft will tell you more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM