Hi all

I am currently sitting with a problem where I have created dynami controls in a seperate "htmlDesigner" calls I have created. Once I return the html to the calling page, I insert the Dynamically created HTML controls within the Page_Init section, which sets the controls onto the controlling page, once this is done, I then try to find the dymaically created "submit button" using the FindControl("btnSubmit") method, in order to assign it an event handler. which returns a null. When debugging I took a look at the raw markup and saw that the submit button is present within the markup but for some reason the FindControl method is not finding it. I have been looking around on the net for a solution to my problem, but to no avail. I reaed that I should generate the markup in the Page_Load, then I read I should generate it in the OnInit section AND also read that I should generate it within the Page_Init, then use the FindControl method......Well..I have tried generating the markup in all these sections and then ran the findcontrol method, but tis still does not work...I know I am missing something, I just don't know what it is....HELP IS DESPERATELY NEEDED..
Below is the code to my HTML markup class
- "SurveyWorker" is a custom static class that is loaded with the current survey title,questions,answers and control types
- "SurveyQuestion" is a custom class that has question properties and methods
- "SurveyAnswers" is a custom answer class that has answer propertis and methods

Please not that the markup generated here is then returned to the calling page....

public static HtmlTextWriter createHtmlSurveyMarkup()
    {


        row = new HtmlTableRow();
        row.VAlign = "top";
        row.Align = "center";

        cell = new HtmlTableCell();

        cell.InnerHtml = "<h3>" + SurveyWorker.Title + "</h3>";
        row.Cells.Add(cell);
        table.Rows.Add(row);


        questions = SurveyWorker.ReturnQuestions();


        foreach (SurveyQuestion q in questions)
        {

            row = new HtmlTableRow();
            cell = new HtmlTableCell();
            row.VAlign = "top";
            row.Align = "left";

            cell.Width = "600";
            Label questionLabel = new Label();
            questionLabel.Width = 800;
            questionLabel.Font.Size = 10;

            questionLabel.Text = "<b>" + q.Question + "</b>";

            
            cell.Controls.Add(questionLabel);
            
            row.Cells.Add(cell);
            //table.Rows.Add(row);
            int count = 1;

            radioList = new RadioButtonList();
            checkList = new CheckBoxList();

            foreach (SurveyAnswer a in q.ReturnAnswerArray())
            {
                
                if (a.ControlType == "Radio Buttons")
                {
                  
                    radioList.ID = a.AnswerID.ToString();
                    radioList.Items.Add(a.AnswerText);

                    cell.Controls.Add(radioList);
                }
                else if (a.ControlType == "Check Boxes")
                {
                   
                    checkList.ID = a.AnswerID.ToString();
                    checkList.Items.Add(a.AnswerText);

                    cell.Controls.Add(checkList);
                }
                else if (a.ControlType == "Text Box")
                {
                    textBox = new TextBox();
                    textBox.ID = a.AnswerID.ToString();
                    textBox.TextMode = TextBoxMode.SingleLine;
                    textBox.Width = 400;
                   
                    cell.Controls.Add(textBox);

                }
                else if (a.ControlType == "Text Area")
                {
                    
                    textBox = new TextBox();
                    textBox.ID = a.AnswerID.ToString();
                    textBox.TextMode = TextBoxMode.MultiLine;
                    textBox.Width = 400;
                    textBox.Height = 40;
                   
                    cell.Controls.Add(textBox);

                }


                if (count == q.ReturnAnswerArray().Count)
                {
                    row.Cells.Add(cell);
                    table.Rows.Add(row);
                }
                count++;
            }
            
            cell.Controls.Add(new LiteralControl("<br />"));
        }


        row = new HtmlTableRow();
        cell = new HtmlTableCell();
        

        //add submit button
        submitButton = new Button();
        submitButton.ID = "btnSubmit";
        submitButton.Text = "Submit";
       

        cell.Controls.Add(submitButton);
        row.Cells.Add(cell);
        table.Rows.Add(row);

        stringWriter = new StringWriter();
        htmlWriter = new HtmlTextWriter(stringWriter);
        table.RenderControl(htmlWriter);
       

        return htmlWriter;
    }

Recommended Answers

All 2 Replies

Are you searching the correct container?

ie if you add it to a panel. Use panelId.FindControl

Thanks for your help...I chose to do it a completely different way...and it works much better..Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.