looping on Page.Controls

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;
}
}
}
}
 
M

Mattia Saccotelli

I did the following:

foreach ...
try {
HtmlControl t = (HtmlControl)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", "");
continue;
} catch {}

the HtmlTextArea is not even recognized as an HtmlControl, it's not there!
 
D

Daniel Carlsson

Try using the Request.Form collection instead.
I think it will contain hidden fields and such too, but should contain all
the data sent at least.

Hope it help
/Dan
 
M

Mattia Saccotelli

Good idea, I'll do it whit the Form object.

I'd like to know the reason of such a strange behavior of the textarea.

Thanks!
 
D

Daniel Carlsson

I dont think ASP creates controls for the Html controls most of the time
(unlike the web form controls, like TextBox) so if its the "Text Area"
control Im surprised you got a control for it at any point. I havent used
those controls much yet though so not certain.

/Dan
 
M

Mattia Saccotelli

Normally it doesn't except you run them as "server" controls
(runat="server")
The strange thing is that the control "disapper" when I insert some text
inside..
I changed them to multiline TextBox btw

Daniel said:
I dont think ASP creates controls for the Html controls most of the time
(unlike the web form controls, like TextBox) so if its the "Text Area"
control Im surprised you got a control for it at any point. I havent used
those controls much yet though so not certain.

/Dan

Good idea, I'll do it whit the Form object.

I'd like to know the reason of such a strange behavior of the textarea.

Thanks!


Daniel said:
Try using the Request.Form collection instead.
I think it will contain hidden fields and such too, but should contain
all the data sent at least.

Hope it help
/Dan

"Mattia Saccotelli" <"m.saccotelli [AT] gruppostratos [DOT] com"> wrote
in message

I did the following:

foreach ...
try {
HtmlControl t = (HtmlControl)(c);
xmlOut += string.Format(xmlTemplate, t.ID, "", "");
continue;
} catch {}

the HtmlTextArea is not even recognized as an HtmlControl, it's not
there!


Mattia Saccotelli wrote:


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;
}
}
} }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,235
Latest member
Top Crypto Podcasts_

Latest Threads

Top