473,804 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to loop all form fields and HtmlEncode text?

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.Req uest in the page base class routine and
then not worry about passing the page context?

Can anyone help me get started?
Nov 18 '05 #1
8 5691
On Tue, 15 Jun 2004 17:11:28 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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.FindContro l("form1").Cont rols.

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.Fin dControl("form1 ").Controls[0]
}
catch
{
try
{
blah = (DropDownList)t his.FindControl ("form1").Contr ols[0]
}
catch
{
...
}
}
-Adam

Nov 18 '05 #2
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.
<as******@inlan dkwpp.com> wrote in message
news:on******** *************** *********@4ax.c om...
On Tue, 15 Jun 2004 17:11:28 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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 throughserver.form?


this.FindContro l("form1").Cont rols.

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.Fin dControl("form1 ").Controls[0]
}
catch
{
try
{
blah = (DropDownList)t his.FindControl ("form1").Contr ols[0]
}
catch
{
...
}
}
-Adam

Nov 18 '05 #3
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.Cur rent.Request.Fo rm
If TypeOf ctl Is TextBox Then
'What do I do here? I want to HtmlEncode the TextBox.Text
End If
Next
End Sub

"AFN" <DE************ ************@ya hoo.com> wrote in message
news:QE******** ********@twiste r.socal.rr.com. ..
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.
<as******@inlan dkwpp.com> wrote in message
news:on******** *************** *********@4ax.c om...
On Tue, 15 Jun 2004 17:11:28 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
I want to have a routine in a page base class that will take all the textfields on a web form, and then HtmlEncode their values.

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


this.FindContro l("form1").Cont rols.

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.Fin dControl("form1 ").Controls[0]
}
catch
{
try
{
blah = (DropDownList)t his.FindControl ("form1").Contr ols[0]
}
catch
{
...
}
}
-Adam


Nov 18 '05 #4
On Tue, 15 Jun 2004 17:49:36 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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.FindContro l("Form1").Cont rols)
{
//use its type string to figure it out
switch(c.ToStri ng())
{
case "System.Web.UI. WebControls.Lab el":
//do something
break;
case
"LiteralControl System.Web.UI.W ebControls.Text Box":
//do something else
break;
default:
Response.Write( c.ToString());
break;
}
// or cast it
try
{
((Label)c).Text = "do something";
}
catch
{
try
{
((TextBox)c).Te xt = "do something else";

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

-Adam
Nov 18 '05 #5
On Tue, 15 Jun 2004 18:04:12 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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.Cur rent.Request.Fo rm
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
Nov 18 '05 #6
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.
<as******@inlan dkwpp.com> wrote in message
news:u4******** *************** *********@4ax.c om...
On Tue, 15 Jun 2004 18:04:12 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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" thecontrol...

Sub EncodeAll() ' in my page base class
Dim ctl as control
For each ctl in HttpContext.Cur rent.Request.Fo rm
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

Nov 18 '05 #7
On Tue, 15 Jun 2004 18:26:43 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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.Cur rent.Request.Fo rm.GetEnumerato r();
while(en.MoveNe xt())
{
Response.Write( Server.HtmlEnco de(en.Current + " = " +
HttpContext.Cur rent.Request.Fo rm[(string)en.Curr ent]));
}

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.HtmlEnco de(HttpContext. Current.Request .Form["TextBox1"])

-adam
Nov 18 '05 #8
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(oLab el.Text);
}
if (oControl is DropDownList)

you ge tthe idea

cheers
Andrew MCSD
"as******@inlan dkwpp.com" wrote:
On Tue, 15 Jun 2004 17:49:36 GMT, "AFN"
<DE************ ************@ya hoo.com> wrote:
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.FindContro l("Form1").Cont rols)
{
//use its type string to figure it out
switch(c.ToStri ng())
{
case "System.Web.UI. WebControls.Lab el":
//do something
break;
case
"LiteralControl System.Web.UI.W ebControls.Text Box":
//do something else
break;
default:
Response.Write( c.ToString());
break;
}
// or cast it
try
{
((Label)c).Text = "do something";
}
catch
{
try
{
((TextBox)c).Te xt = "do something else";

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

-Adam

Nov 18 '05 #9

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

Similar topics

11
8760
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding if they left any field blank. My guess is that someone is trying to hack the site using the form to gain entry or run commands -- I don't really know since I'm not a hacker. I just know that forms are often susceptible to these kinds of...
3
2284
by: NotGiven | last post by:
I have a web form with several fields. If I copy & paste from a RTF document into a field, the javascript validation and field length are bypassed and cause the form to fail. Any ideas?
2
4084
by: Nick | last post by:
Loop to create an array from a dynamic form. I'm having trouble with an application, and I'll try to explain it as clearly as possible: 1. I have a form with two fields, say Apples and Oranges. The user selects from a drop down menu, between 1-10 of each and clicks submit. The resulting page will display a NEW form, with rows and a list of fields for the amount of each items selected.
4
9907
by: karenmiddleol | last post by:
I have the following form the user enters the From and to period and presses the Submit button and the form fields are cleared once the submit button is pressed. Is there a way I can keep the Form fields visible and not cleared. Also when I display the message I want the original form cleared in a different version of this page how can I clear the form completely when I display the data entered from the Response statements I do not...
3
2119
by: Nelson R. | last post by:
Hi, im using a form to get some input from the user. This form is in a HTML file. When I post the form directly to my email, i receive all fields correctly. Example test.html: <FORM action="MAILTO:myemail@work.com" method=post enctype="text/plain">
13
3635
by: johnemmatty | last post by:
I am using an asp page in which i dynamically fill the ACTION property of the form. The problem is that whenever i try to redirect to a html page using the javascript:location, it is getting redirected to the location in the ACTION attribute of the form element despite what is assigned to the location attribute of the javascript. <FORM NAME="CRYSTFORM" METHOD=POST ACTION=<%=thisPage%>> : : :
16
2857
by: Jen | last post by:
Hi. I have this problem that I think should be easy but have been struggling with this for days. I have a list based on a recordset from a database. This list consists of records meeting a certain criteria and at the bottom of this list i have a button that inserts all these records to a´nother table in the database. So long everything's ok. BUT, at the top of this list I have a textarea that the user can write down some text to be put...
3
4927
by: hugo | last post by:
Hi, I have a function that I call from form fields using the OnKeyUp function to replace special caracters. Once this function has been called, it does not set the focus on the form field where I have called the function from. What happens : I laod a EDIT page with a html form on it and fill in the fields with
60
5285
by: bonneylake | last post by:
Hey Everyone, What i am trying to do is show previously entered information. I know i need a loop for this , but i am not sure how to loop through this based on my form appears all in the javascript here is my code <cfquery name="serial" datasource="CustomerSupport"> SELECT pka_serialNo,pkb_fk_ticketNo,model_no,product_type,software_hardware,resolution,resolution_date, ...
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.