how to loop all form fields and HtmlEncode text?

A

AFN

I want to have a routine in a page base class that will take all the text
fields on a web form, and then HtmlEncode their values.

I'm having trouble figuring out if I want to loop controls or loop through
server.form?

I'm also having trouble figuring out which ones are text fields (versus,
say, pulldowns)?

I'm also having trouble deciding if I need to pass context to my base class
routine or just use HttpContext.Request in the page base class routine and
then not worry about passing the page context?

Can anyone help me get started?
 
A

ashelley

I want to have a routine in a page base class that will take all the text
fields on a web form, and then HtmlEncode their values.

I'm having trouble figuring out if I want to loop controls or loop through
server.form?
this.FindControl("form1").Controls.


I'm also having trouble figuring out which ones are text fields (versus,
say, pulldowns)?

You can do a try catch fall through

try
{
blah = (Label)this.FindControl("form1").Controls[0]
}
catch
{
try
{
blah = (DropDownList)this.FindControl("form1").Controls[0]
}
catch
{
...
}
}


-Adam
 
A

AFN

thank you for the reply. I'm still having trouble understanding how I would
iterate through the controls, though. Maybe I'm missing something. I'm
using VB.NET, but I can understand C# too.


I want to have a routine in a page base class that will take all the text
fields on a web form, and then HtmlEncode their values.

I'm having trouble figuring out if I want to loop controls or loop through
server.form?
this.FindControl("form1").Controls.


I'm also having trouble figuring out which ones are text fields (versus,
say, pulldowns)?

You can do a try catch fall through

try
{
blah = (Label)this.FindControl("form1").Controls[0]
}
catch
{
try
{
blah = (DropDownList)this.FindControl("form1").Controls[0]
}
catch
{
...
}
}


-Adam
 
A

AFN

sorry, let me be more specific. here's my code (which I haven't tested
yet), but I don't know how to set the control's text value once I "have" the
control...

Sub EncodeAll() ' in my page base class
Dim ctl as control
For each ctl in HttpContext.Current.Request.Form
If TypeOf ctl Is TextBox Then
'What do I do here? I want to HtmlEncode the TextBox.Text
End If
Next
End Sub





AFN said:
thank you for the reply. I'm still having trouble understanding how I would
iterate through the controls, though. Maybe I'm missing something. I'm
using VB.NET, but I can understand C# too.


I want to have a routine in a page base class that will take all the text
fields on a web form, and then HtmlEncode their values.

I'm having trouble figuring out if I want to loop controls or loop through
server.form?
this.FindControl("form1").Controls.


I'm also having trouble figuring out which ones are text fields (versus,
say, pulldowns)?

You can do a try catch fall through

try
{
blah = (Label)this.FindControl("form1").Controls[0]
}
catch
{
try
{
blah = (DropDownList)this.FindControl("form1").Controls[0]
}
catch
{
...
}
}


-Adam
 
A

ashelley

thank you for the reply. I'm still having trouble understanding how I would
iterate through the controls, though. Maybe I'm missing something. I'm
using VB.NET, but I can understand C# too.

foreach(Control c in this.FindControl("Form1").Controls)
{
//use its type string to figure it out
switch(c.ToString())
{
case "System.Web.UI.WebControls.Label":
//do something
break;
case
"LiteralControlSystem.Web.UI.WebControls.TextBox":
//do something else
break;
default:
Response.Write(c.ToString());
break;
}
// or cast it
try
{
((Label)c).Text = "do something";
}
catch
{
try
{
((TextBox)c).Text = "do something else";

catch
{
Response.Write(c.ToString());
}
}
}

-Adam
 
A

ashelley

sorry, let me be more specific. here's my code (which I haven't tested
yet), but I don't know how to set the control's text value once I "have" the
control...

Sub EncodeAll() ' in my page base class
Dim ctl as control
For each ctl in HttpContext.Current.Request.Form
If TypeOf ctl Is TextBox Then
'What do I do here? I want to HtmlEncode the TextBox.Text
End If
Next
End Sub

You are looping through data posted to the web page not through the
controls. These are all strings. I don't believe u even "set" these
values.

-Adam
 
A

AFN

sorry, maybe I'm being dumb, but I WANT to loop through the submitted string
data. I want a routine that will take all submitted user textbox data, and
run HtmlEncode on each TextBox.Text value before I save to a database. What
would I do differently with my vb.net code example below?

On the line where I put my comment, I can't say ctl.Text =
HtmlEncode(ctl.Text). It doesn't recognize the "Text" property.
 
A

ashelley

sorry, maybe I'm being dumb, but I WANT to loop through the submitted string
data. I want a routine that will take all submitted user textbox data, and
run HtmlEncode on each TextBox.Text value before I save to a database. What
would I do differently with my vb.net code example below?
not at all dumb, i just misunderstood the problem, sorry about that.

IEnumerator en = HttpContext.Current.Request.Form.GetEnumerator();
while(en.MoveNext())
{
Response.Write(Server.HtmlEncode(en.Current + " = " +
HttpContext.Current.Request.Form[(string)en.Current]));
}

the key thing is that en.Current will be the name of the field. You
will have to selectively store these. Since you have to selectively
pick out your data anyways chances are you already know what fields
you want so all of this can be done like this

Server.HtmlEncode(HttpContext.Current.Request.Form["TextBox1"])

-adam
 
G

Guest

You can loop through the Form post data to find the values but it is sometimes difficult to figure out which controls the data comes from if it is embedded in parent controls that implement the NamingContainer.

I have one suggestion for the code:
Instead of using the Type String to decide how to cast the controls and definitely instead of using a cast and throwing an exception during 'normal' processing (an MCSD tact no-no) why not use the "IS" operator.

....from memory :) ...

if (oControl is Label)
{
Label oLabel = (Label)oControl;
//HTMLEncode(oLabel.Text);
}
if (oControl is DropDownList)

you ge tthe idea

cheers
Andrew MCSD
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top