M
Mattia Saccotelli
Hi to all. I'm trying to auto-save a form to XML using Page.Controls
property. Almost everything is fine, except a really weird behavior with
the HtmlTextArea control. I noticed that if it has a value (some text
inside!) it's NOT recognized as an HtmlTextArea in the loop, while if
it's blank the condition hits. Btw using FindControl the text area is
correctly recognized and the value is there.
How is it possible? Does the "is" work?
Any suggestion?
Here is the code:
private void saveControlsToXml(Control parent, ref string xmlOut) {
string xmlTemplate =
"<Control><ID>{0}</ID><Text>{1}</Text><Value>{2}</Value></Control>";
foreach (Control c in parent.Controls) {
if (c.HasControls()) {
saveControlsToXml(c, ref xmlOut);
}
else {
if (c is System.Web.UI.HtmlControls.HtmlTextArea) {
HtmlTextArea t = (HtmlTextArea)(c);
xmlOut += string.Format(xmlTemplate, t.ID, t.InnerText, "");
continue;
}
if (c is System.Web.UI.WebControls.TextBox) {
TextBox t = (TextBox)(c);
xmlOut += string.Format(xmlTemplate, t.ID, t.Text, "");
continue;
}
if (c is System.Web.UI.WebControls.DropDownList) {
DropDownList d = (DropDownList)(c);
if (d.SelectedItem != null) {
xmlOut += string.Format(xmlTemplate, d.ID, d.SelectedItem.Text,
d.SelectedValue);
}
continue;
}
if (c is System.Web.UI.WebControls.RadioButton) {
RadioButton t = (RadioButton)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", t.Checked.ToString());
continue;
}
if (c is System.Web.UI.HtmlControls.HtmlInputText) {
HtmlInputText t = (HtmlInputText)(c);
xmlOut += string.Format(xmlTemplate, t.ID, t.Value, "");
continue;
}
if (c is System.Web.UI.HtmlControls.HtmlInputCheckBox) {
HtmlInputCheckBox t = (HtmlInputCheckBox)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", t.Checked.ToString());
continue;
}
if (c is System.Web.UI.HtmlControls.HtmlInputRadioButton) {
HtmlInputRadioButton t = (HtmlInputRadioButton)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", t.Checked.ToString());
continue;
}
}
}
}
property. Almost everything is fine, except a really weird behavior with
the HtmlTextArea control. I noticed that if it has a value (some text
inside!) it's NOT recognized as an HtmlTextArea in the loop, while if
it's blank the condition hits. Btw using FindControl the text area is
correctly recognized and the value is there.
How is it possible? Does the "is" work?
Any suggestion?
Here is the code:
private void saveControlsToXml(Control parent, ref string xmlOut) {
string xmlTemplate =
"<Control><ID>{0}</ID><Text>{1}</Text><Value>{2}</Value></Control>";
foreach (Control c in parent.Controls) {
if (c.HasControls()) {
saveControlsToXml(c, ref xmlOut);
}
else {
if (c is System.Web.UI.HtmlControls.HtmlTextArea) {
HtmlTextArea t = (HtmlTextArea)(c);
xmlOut += string.Format(xmlTemplate, t.ID, t.InnerText, "");
continue;
}
if (c is System.Web.UI.WebControls.TextBox) {
TextBox t = (TextBox)(c);
xmlOut += string.Format(xmlTemplate, t.ID, t.Text, "");
continue;
}
if (c is System.Web.UI.WebControls.DropDownList) {
DropDownList d = (DropDownList)(c);
if (d.SelectedItem != null) {
xmlOut += string.Format(xmlTemplate, d.ID, d.SelectedItem.Text,
d.SelectedValue);
}
continue;
}
if (c is System.Web.UI.WebControls.RadioButton) {
RadioButton t = (RadioButton)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", t.Checked.ToString());
continue;
}
if (c is System.Web.UI.HtmlControls.HtmlInputText) {
HtmlInputText t = (HtmlInputText)(c);
xmlOut += string.Format(xmlTemplate, t.ID, t.Value, "");
continue;
}
if (c is System.Web.UI.HtmlControls.HtmlInputCheckBox) {
HtmlInputCheckBox t = (HtmlInputCheckBox)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", t.Checked.ToString());
continue;
}
if (c is System.Web.UI.HtmlControls.HtmlInputRadioButton) {
HtmlInputRadioButton t = (HtmlInputRadioButton)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", t.Checked.ToString());
continue;
}
}
}
}