I generate select control(Q1DDL and Q2DDL) in the innerHTML of a div divQ1AnswerDDLSub in the pageload
The code is perfectly generated as desired.
However, the findControl of ASP.net is not able to find the generated HTMLSELECT control (Q1DDL and Q2DDL)from the .aspx page after I press the SUBMIT Button.
The runtime HTMLSelect control generation
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
return;
}
....
if (bool.Parse(questions[QNo-1].AnswerDropDownList.visibleDDl))
{
(FindControl("lblQ" + QNo + "AnswerDDL") as HtmlGenericControl).InnerText = questions[QNo-1].AnswerDropDownList.titleDDL;
(FindControl("fsQ" + QNo + "DDL") as HtmlGenericControl).Style["border"] = questions[QNo - 1].AnswerDropDownList.FieldsetDDL == "no" ? "0px" : null;
var licDDL = questions[QNo-1].AnswerDropDownList.optionDDL;
var builder = new System.Text.StringBuilder();
for (int i = 0; i < licDDL.Length; i++)
{
builder.Append(String.Format("<option value='{0}' title='{1}' >{1}</option> ", licDDL[i].value, licDDL[i].text));
}
if (questions[QNo-1].AnswerDropDownList.required == "yes")
{
(FindControl("divQ" + QNo + "AnswerDDLSub") as HtmlGenericControl).InnerHtml = "<select runat=\"server\" name=\"Q" + QNo + "DDL\" id=\"Q" + QNo + "DDL\" required=\"required\">" + builder.ToString() + "</select>";
}
else
{
(FindControl("divQ" + QNo + "AnswerDDLSub") as HtmlGenericControl).InnerHtml = "<select runat=\"server\" name=\"Q" + QNo + "DDL\" id=\"Q" + QNo + "DDL\">" + builder.ToString() + "</select>";
}
}
The generated code when i view the page source
<div id="divQ1AnswerDDL">
<fieldset id="fsQ1DDL" style="border:;">
<legend>
<label id="lblQ1AnswerDDL">Title of Drop Down List </label>
</legend>
<div id="divQ1AnswerDDLSub"><select runat="server" name="Q1DDL" id="Q1DDL" required="required"><option value='' title='Select' >Select</option> <option value='IE' title='IE' >IE</option> <option value='Safari' title='Safari' >Safari</option> <option value='Chrome' title='Chrome' >Chrome</option> </select></div>
</fieldset>
</div>
in the code behind
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
for (int QNo = 1; QNo < questions.Length + 1; QNo++)
{
ADDLRequired = (FindControl("Q" + QNo + "DDL") as HtmlSelect).Attributes["required"] == "required" ? true: false,
}
}
(FindControl("Q" + QNo + "DDL") as HtmlSelect) <== is showing null