how to loop all form fields and HtmlEncode text?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AFN

    how to loop all form fields and HtmlEncode text?

    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?


  • ashelley@inlandkwpp.com

    #2
    Re: how to loop all form fields and HtmlEncode text?

    On Tue, 15 Jun 2004 17:11:28 GMT, "AFN"
    <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
    [color=blue]
    >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?[/color]

    this.FindContro l("form1").Cont rols.
    [color=blue]
    >
    >I'm also having trouble figuring out which ones are text fields (versus,
    >say, pulldowns)?[/color]

    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

    Comment

    • AFN

      #3
      Re: how to loop all form fields and HtmlEncode text?

      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.


      <ashelley@inlan dkwpp.com> wrote in message
      news:onbuc0l9d3 gqmt5lcdt3d1l6k m1c7i1vp0@4ax.c om...[color=blue]
      > On Tue, 15 Jun 2004 17:11:28 GMT, "AFN"
      > <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
      >[color=green]
      > >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[/color][/color]
      through[color=blue][color=green]
      > >server.form?[/color]
      >
      > this.FindContro l("form1").Cont rols.
      >[color=green]
      > >
      > >I'm also having trouble figuring out which ones are text fields (versus,
      > >say, pulldowns)?[/color]
      >
      > 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
      >[/color]


      Comment

      • AFN

        #4
        Re: how to loop all form fields and HtmlEncode text?

        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" <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote in message
        news:QEGzc.1996 $fd.1045@twiste r.socal.rr.com. ..[color=blue]
        > thank you for the reply. I'm still having trouble understanding how I[/color]
        would[color=blue]
        > iterate through the controls, though. Maybe I'm missing something. I'm
        > using VB.NET, but I can understand C# too.
        >
        >
        > <ashelley@inlan dkwpp.com> wrote in message
        > news:onbuc0l9d3 gqmt5lcdt3d1l6k m1c7i1vp0@4ax.c om...[color=green]
        > > On Tue, 15 Jun 2004 17:11:28 GMT, "AFN"
        > > <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
        > >[color=darkred]
        > > >I want to have a routine in a page base class that will take all the[/color][/color][/color]
        text[color=blue][color=green][color=darkred]
        > > >fields on a web form, and then HtmlEncode their values.
        > > >
        > > >I'm having trouble figuring out if I want to loop controls or loop[/color][/color]
        > through[color=green][color=darkred]
        > > >server.form?[/color]
        > >
        > > this.FindContro l("form1").Cont rols.
        > >[color=darkred]
        > > >
        > > >I'm also having trouble figuring out which ones are text fields[/color][/color][/color]
        (versus,[color=blue][color=green][color=darkred]
        > > >say, pulldowns)?[/color]
        > >
        > > 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
        > >[/color]
        >
        >[/color]


        Comment

        • ashelley@inlandkwpp.com

          #5
          Re: how to loop all form fields and HtmlEncode text?

          On Tue, 15 Jun 2004 17:49:36 GMT, "AFN"
          <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
          [color=blue]
          >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.
          >[/color]

          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

          Comment

          • ashelley@inlandkwpp.com

            #6
            Re: how to loop all form fields and HtmlEncode text?

            On Tue, 15 Jun 2004 18:04:12 GMT, "AFN"
            <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
            [color=blue]
            >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
            >[/color]

            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

            Comment

            • AFN

              #7
              Re: how to loop all form fields and HtmlEncode text?

              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.


              <ashelley@inlan dkwpp.com> wrote in message
              news:u4euc0lq3t 6kd8knsf9fp3pe1 88979l602@4ax.c om...[color=blue]
              > On Tue, 15 Jun 2004 18:04:12 GMT, "AFN"
              > <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
              >[color=green]
              > >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"[/color][/color]
              the[color=blue][color=green]
              > >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
              > >[/color]
              >
              > 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[/color]


              Comment

              • ashelley@inlandkwpp.com

                #8
                Re: how to loop all form fields and HtmlEncode text?

                On Tue, 15 Jun 2004 18:26:43 GMT, "AFN"
                <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
                [color=blue]
                >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?
                >[/color]
                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


                Comment

                • Andy Z Smith

                  #9
                  Re: how to loop all form fields and HtmlEncode text?

                  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


                  "ashelley@inlan dkwpp.com" wrote:
                  [color=blue]
                  > On Tue, 15 Jun 2004 17:49:36 GMT, "AFN"
                  > <DELETEnewsgrou pCAPSaccount@ya hoo.com> wrote:
                  >[color=green]
                  > >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.
                  > >[/color]
                  >
                  > 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
                  >[/color]

                  Comment

                  Working...