Form ids in a content placeholder

Discussion in 'ASP .Net' started by Bob, Feb 25, 2008.

  1. Bob

    Bob Guest

    I am trying to obtain values of a submitted form when the form is inside a
    content placeholder via a master page. For example:

    <asp:content contentplaceholderid="contentMain" runat="server">
    <asp:TextBox runat="server" id="FirstAndLastName"></asp:TextBox>

    However, the identifier for the form element in Request.Form after the form
    is submitted to another page is:


    Is there a way to prevent form identifers from prefacing the content
    placeholder id? If not, on the form receiving page, how do I obtain the
    field? Request.Form["FirstAndLastName"] doesn't work, I am required to
    prepend the ctl100$contentMain$ stuff.

    Bob, Feb 25, 2008
    1. Advertisements

  2. Reference the Control on the server side, rather than the HTML element it
    creates on the client.


    Kevin Spencer
    Chicken Salad Surgeon
    Microsoft MVP
    Kevin Spencer, Feb 25, 2008
    1. Advertisements

  3. Bob

    bruce barker Guest

    create a hidden field that you store the content place holders UniqueID. then
    you will know what to prepend to the controls name. you could also name the
    container (in the codebehind) so it had a fixed name rather than ctl100.

    -- bruce (
    bruce barker, Feb 25, 2008
  4. Bob

    Bob Guest

    The form has this structure:

    <%@ page language="C#" masterpagefile="~/templates/site.master"
    codefile="~/myform.cs" inherits="MyCompany.MyForm" title="My Form" %>
    <%@ mastertype typename="MyCompany.MasterPage" %>
    <asp:content contentplaceholderid="contentMain" runat="server">
    <form id="myForm" runat="server">
    <asp:TextBox runat="server" id="FirstAndLastName"></asp:TextBox>
    <asp:Button runat="server" Text="Submit" id="Submit" onclick="DoSubmit" />

    Then in the myform.cs file, I am doing some validation in DoSubmit() and
    then if all is OK I do Server.Transfer("/postsubmit.aspx");

    Inside postsubmit.aspx is where I am trying to do
    Request.Form["FirstAndLastName"], but it wants me to add ctl100. The only
    way I have found to get around this is to move the control text (like Kevin
    says below) into Context.Items[]. Then do the Server.Transfer() and in the
    other page in PreRender event I push all the Context.Items[] into the
    ViewState[]. It seems pretty silly to have to move stuff around like this
    when I know it is in Request.Form[].

    Is there a better way to do this?


    Bob, Feb 25, 2008
  5. Hi Bob,

    No need to move anything. The HttpContext.Current property contains a
    property called "Handler" which is the Page that initially handled the
    Request. Cast it as the Page class that handled the Request, and then you
    can get a handle on all the properties of the Page.

    string s = ((myform)HttpContext.Current.Handler).FirstAndLastName.Text;


    Kevin Spencer
    Chicken Salad Surgeon
    Microsoft MVP

    Kevin Spencer, Feb 26, 2008
  6. Bob

    clintonG Guest

    Yea Bob, I think there is a "better way" noting I rarely have an opportunity
    to suggest something other than what Kevin has to offer. Start by learning
    how the page is actually compiled for instance eh? Then you'll have insight
    into how and why the control tree is generated and why we see the ctl
    prefixed notation. In addition, the MasterPage adds a layer of abstraction
    as it is a unique type of user control that the compiler adds to the content
    page at runtime.

    So no, there is no way around the ctl prefix; we must work with the control
    tree on its own terms.

    Secondly, FirstAndLastName as it is being used in this context is not a
    field; it is a property (more on this in a moment).

    My work with Master Pages often compels me to enable trace in the page as a
    way to read and understand the control tree in the context it has been
    compiled. Once the property has been located and read from the control tree
    it can be referenced using the FindControl method. The late bound
    FindControl method can get really ugly.

    It is more elegant and literally more efficient at runtime to reference the
    control using a public property (that's where a field will be used, e.g.
    private _firstandlastname;). Doing so allows us to access the objectified
    instance of the now early bound control by referencing its public properties

    Finally, you can wade into the deep side of the Master Pages pool by taking
    advantage of some outstanding work from a guy named K. Scott Allen at

    <%= Clinton Gallagher

    clintonG, Feb 27, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.