473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

looping on Page.Controls

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;
}
}
}
}
Nov 19 '05 #1
5 1403
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;
}
}
}
}

Nov 19 '05 #2
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 news:%2****************@TK2MSFTNGP09.phx.gbl...
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;
}
}
} }

Nov 19 '05 #3
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 Carlsson wrote:
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 news:%2****************@TK2MSFTNGP09.phx.gbl...
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;
}
}
} }


Nov 19 '05 #4
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

"Mattia Saccotelli" <"m.saccotelli [AT] gruppostratos [DOT] com"> wrote in
message news:uf**************@TK2MSFTNGP10.phx.gbl...
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 Carlsson wrote:
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 news:%2****************@TK2MSFTNGP09.phx.gbl...
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;
}
}
} }



Nov 19 '05 #5
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 Carlsson wrote:
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

"Mattia Saccotelli" <"m.saccotelli [AT] gruppostratos [DOT] com"> wrote in
message news:uf**************@TK2MSFTNGP10.phx.gbl...
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 Carlsson wrote:
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 news:%2****************@TK2MSFTNGP09.phx.gbl...
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;
>}
>}
>} }

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
7
by: Hai Nguyen | last post by:
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls)...
2
by: Shawn | last post by:
Hi. I'm trying to loop through all webcontrols in my System.Web.UI.HtmlControls.HtmlForm. This is my code: Dim form As HtmlForm Dim control As Control form = Me.FindControl("completion")...
5
by: Craig G | last post by:
how do i go about this thru serverside code (VB.NET)? any links to any articles anywhere? basically i just want something simple that will loop thru all txt & cbo server side controls, and then...
6
by: Craig G | last post by:
i have the routine setup as below. the ctlCollection is populated with Page.Controls. but it only ever brings back 3 controls even though there is more than this on the page the control types it...
2
by: Asha | last post by:
hello, i got many asp.net controls and i want to put the same values into all of the contorls. is there a better coding habit to allow me to loop through all these contorls with different id to...
7
by: Jim Bancroft | last post by:
Hi everyone, I'm struggling with something.....I'd like to loop over all of my page's HyperLink controls, and I'm not sure how to do it. Here's what I'm looking for, in pseudocode: foreach...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
1
by: tshad | last post by:
I was trying to do this in Javascript but found I can't do spans (which is what Labels turn into). I can do a loop thru a control list something like: Public Sub LoopingControls(ByVal oControl...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.