Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Form ids in a content placeholder

Reply
Thread Tools

Form ids in a content placeholder

 
 
Bob
Guest
Posts: n/a
 
      02-25-2008
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


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      02-25-2008
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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
>
>



 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      02-25-2008
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
>
>
>

 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-25-2008
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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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
>>
>>

>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      02-26-2008
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).FirstAndLast Name.Text;

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Bob" <(E-Mail Removed)> wrote in message
news:uii0QA%(E-Mail Removed)...
> 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:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>>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
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
clintonG
Guest
Posts: n/a
 
      02-27-2008
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:(E-Mail Removed)...
> 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).FirstAndLast Name.Text;
>
> --
> HTH,
>
> Kevin Spencer
> Chicken Salad Surgeon
> Microsoft MVP
>
> "Bob" <(E-Mail Removed)> wrote in message
> news:uii0QA%(E-Mail Removed)...
>> 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:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:%(E-Mail Removed)...
>>>>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
>>>>
>>>>
>>>
>>>

>>
>>

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
Missing Content Placeholder in nested master content page John ASP .Net 2 01-15-2008 11:48 AM
Simple question: Retrieve form elements name in content placeholder Kees de Winter ASP .Net 3 05-12-2006 03:40 PM
after transfer of data from MS-outlook(mail ids) to application,mail ids are consisting of strange characters vamsikrishna_b@coolgoose.com Python 2 06-21-2005 12:41 PM
Build control in a placeholder or like a placeholder nail ASP .Net Building Controls 0 09-10-2004 07:57 PM



Advertisments