Phani Kumar

Phani Kumar

  • NA
  • 162
  • 24.4k

while accessing values from dynamic controls getting error

Mar 5 2016 2:44 AM
Hi All,
 
I have created dynamic textboxes in a panel and trying to save them into database as a XML file, but I am receiving an error like 

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControl'

Here is my piece of code which I have written
 
StringBuilder _StrResult = new StringBuilder();
_StrResult.Append("<root>");
foreach (TextBox txt in pnlInterview.Controls)
{
foreach (Label lbl in pnlInterview.Controls)
{
Label lblLevel = (Label)pnlInterview.FindControl("lbl");
TextBox txtInterviewRound = (TextBox)pnlInterview.FindControl("txt");
_StrResult.Append("<InterviewLevel>");
_StrResult.Append("<level>" + lblLevel.Text.ToString() + "</level>");
_StrResult.Append("<RoundName>" + txtInterviewRound.Text.ToString() + "</RoundName>");
_StrResult.Append("</InterviewLevel>");
}
}
_StrResult.Append("</root>");
return _StrResult;
 
Unable to recognize where it went wrong
 
I am unable to access oftype(<Textbox>)
 
at starting foreach loop only I am receiving this error 
 
any help is appreciated
 

Answers (9)