9
Answers

while accessing values from dynamic controls getting error

Phani Kumar

Phani Kumar

8y
552
1
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)