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:

    ctl00%24contentMain%24FirstAndLastName

    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.

    Thanks
     
    Bob, Feb 25, 2008
    #1
    1. Advertising

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

    --
    HTH,

    Kevin Spencer
    Chicken Salad Surgeon
    Microsoft MVP

    "Bob" <> wrote in message
    news:%...
    >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:
    >
    > ctl00%24contentMain%24FirstAndLastName
    >
    > 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.
    >
    > Thanks
    >
    >
     
    Kevin Spencer, Feb 25, 2008
    #2
    1. Advertising

  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 (sqlwork.com)


    "Bob" wrote:

    > 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:
    >
    > ctl00%24contentMain%24FirstAndLastName
    >
    > 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.
    >
    > Thanks
    >
    >
    >
     
    bruce barker, Feb 25, 2008
    #3
  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" />
    </form>
    </asp:content>

    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?

    Thanks


    "Kevin Spencer" <unclechutney@localhost> wrote in message
    news:...
    > Reference the Control on the server side, rather than the HTML element it
    > creates on the client.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Chicken Salad Surgeon
    > Microsoft MVP
    >
    > "Bob" <> wrote in message
    > news:%...
    >>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:
    >>
    >> ctl00%24contentMain%24FirstAndLastName
    >>
    >> 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.
    >>
    >> Thanks
    >>
    >>

    >
    >
     
    Bob, Feb 25, 2008
    #4
  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;

    --
    HTH,

    Kevin Spencer
    Chicken Salad Surgeon
    Microsoft MVP

    "Bob" <> wrote in message
    news:uii0QA%...
    > 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" />
    > </form>
    > </asp:content>
    >
    > 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?
    >
    > Thanks
    >
    >
    > "Kevin Spencer" <unclechutney@localhost> wrote in message
    > news:...
    >> Reference the Control on the server side, rather than the HTML element it
    >> creates on the client.
    >>
    >> --
    >> HTH,
    >>
    >> Kevin Spencer
    >> Chicken Salad Surgeon
    >> Microsoft MVP
    >>
    >> "Bob" <> wrote in message
    >> news:%...
    >>>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:
    >>>
    >>> ctl00%24contentMain%24FirstAndLastName
    >>>
    >>> 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.
    >>>
    >>> Thanks
    >>>
    >>>

    >>
    >>

    >
    >
     
    Kevin Spencer, Feb 26, 2008
    #5
  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
    directly.

    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
    http://odetocode.com/.

    <%= Clinton Gallagher

    "Kevin Spencer" <unclechutney@localhost> wrote in message
    news:...
    > 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;
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Chicken Salad Surgeon
    > Microsoft MVP
    >
    > "Bob" <> wrote in message
    > news:uii0QA%...
    >> 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"
    >> />
    >> </form>
    >> </asp:content>
    >>
    >> 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?
    >>
    >> Thanks
    >>
    >>
    >> "Kevin Spencer" <unclechutney@localhost> wrote in message
    >> news:...
    >>> Reference the Control on the server side, rather than the HTML element
    >>> it creates on the client.
    >>>
    >>> --
    >>> HTH,
    >>>
    >>> Kevin Spencer
    >>> Chicken Salad Surgeon
    >>> Microsoft MVP
    >>>
    >>> "Bob" <> wrote in message
    >>> news:%...
    >>>>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:
    >>>>
    >>>> ctl00%24contentMain%24FirstAndLastName
    >>>>
    >>>> 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.
    >>>>
    >>>> Thanks
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    clintonG, Feb 27, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kees de Winter
    Replies:
    3
    Views:
    1,008
    Karl Seguin [MVP]
    May 12, 2006
  2. Replies:
    2
    Views:
    731
    Beat Bolli
    Jun 21, 2005
  3. John
    Replies:
    2
    Views:
    947
  4. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    372
  5. Abraham Luna

    control ids on rendered content pages

    Abraham Luna, Sep 2, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    269
    clintonG
    Sep 2, 2005
Loading...

Share This Page