SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Renaming form field names

    Is there a way to undo what asp.net does with form field names? For example, fields that I name:

    <asp:TextBox ID="first_name" runat="server" />
    <asp:TextBox ID="last_name" runat="server" />
    <asp:TextBox ID="title" runat="server" />

    come back as :

    <input name="ctl00$bodycontentleft$first_name" type="text" id="ctl00_bodycontentleft_first_name" />
    <input name="ctl00$bodycontentleft$last_name" type="text" id="ctl00_bodycontentleft_last_name" />
    <input name="ctl00$bodycontentleft$title" type="text" id="ctl00_bodycontentleft_title" />


    Appending the "ctl00$bodycontentleft$" causes an array of problems depending on what you are trying to do with the form fields, especially if the form is sometimes being posted to an external site. Any ideas?

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah - if you want to process fields the "old way" simply don't use server side controls. Instead use a plain form (no runat="server") and use regular <input> elements (no runat="server").

  3. #3
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,875
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    asp.net renames al the IDs to ensure there are no dupilcates. If you do not need to access them from C#/VB u can do as honeymonster says and not use it, if u do need it, then u must unfortunatley live with it. The other site you are posting to will need to handle them as is or just loop the the Request.QueryString collection.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not strictly true that you have to live with it. You can override the prerender event of the page and manipulate the html before it is rendered in the browser. You can expand this to a handler or module.

  5. #5
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I elect to not use the server controls and just stick with plain old form input elements, how would I access them in my code behind file?

    Quote Originally Posted by NightStalker-DNS View Post
    asp.net renames al the IDs to ensure there are no dupilcates. If you do not need to access them from C#/VB u can do as honeymonster says and not use it, if u do need it, then u must unfortunatley live with it. The other site you are posting to will need to handle them as is or just loop the the Request.QueryString collection.

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Request.Form["MyControlId"].

    That said, if you are posting to an external site, you aren't using server-side code on your site to handle anything.

  7. #7
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.

    Believe it or not, this form I am working can post to an external site or use server-side code. In this case, the form action is changed using Javascript depending on a choice made in a dropdown (select) box. If the user chooses choice "A" the form results need to just be emailed to internal staff. If the user chooses choice "B", then the results need to be posted to the external site. And for the external site, the fields must be specific names. Thus my dilemma.

    Quote Originally Posted by wwb_99 View Post
    Request.Form["MyControlId"].

    That said, if you are posting to an external site, you aren't using server-side code on your site to handle anything.

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Well, you could just grab the data, form the request and use System.Net.WebRequest() to post the form data. You get the added benefits of being able to validate/scrub the data on your end before it goes, and to gracefully handle the other site's errors, etc.

    If you still want to do it in JS, perhaps you could just swap in an entirely new form?

  9. #9
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow I did not know you could post form data using System.Net.WebRequest(). I will definitely have to look into that more as I have many forms to do that need to be able to post internal and external and some have will have more complex needs where I will want to use the .NET server controls. For now I have deadline approaching though, so the Request.Form gives me a solution for the time being.

    Quote Originally Posted by wwb_99 View Post
    Well, you could just grab the data, form the request and use System.Net.WebRequest() to post the form data. You get the added benefits of being able to validate/scrub the data on your end before it goes, and to gracefully handle the other site's errors, etc.

    If you still want to do it in JS, perhaps you could just swap in an entirely new form?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •