views:

144

answers:

4

Hi

I have a master page which has a <form runat="server"> line and a ContentPlaceHolder in it.

I am trying create a content page using this master page whitch has text boxes in it. But I cannot reach values of theese text boxes using Request.Form["textbox"] from that conent page. I need to use Request.Form["ctl00$ContentPlaceHolder1$textbox"].

Is this the normal way to do it? If not what might be I am doing wrong?

By the way I am using same content page to process form values.

So I guess my question is actually: How can I access form values of a Content Page within the same content page?

+1  A: 

Assuming you have standard ASP.Net controls, you can either access the control's value in the code behind by using

Dim x as string = Me.txtMyTextBox.Text

If you want to use it in script it's very similar

<%

Dim x as string = Me.txtMyTextBox.Text

%>

You shouldn't need to use Request.Form because the values of these controls are maintained in the ViewState of the page.

Joel Etherton
Thank you. Next time I should web forms trying to emulate windows forms. My mind still works as HTML terms.
prometeus
@prometeus: If you already know about HTML and HTTP, you might want to consider ASP.NET MVC as an alternative to Web Forms.
Jørn Schou-Rode
A: 

You can make the controls accessible, take them out of the designer and make them public in your master page's code-behind file:

public MyMasterPageClass 
{
  public TextBox textbox;
}

To access it in the content page:

var text = ((MyMasterPageClass )Master).textbox.Text;

Or alternatively, in your contentpage's markup use the @MasterType directive:

<%@ MasterType VirtualPath="~/masters/MyMasterPage.master”" %>

And in the page, you don't need the cast:

var text = Master.textbox.Text;
Nick Craver
A: 

You could make a usercontrol (containing the text box) then add a public property to this control which returns the string value of the text box. Replace the text box on the master page with this user control and then you can get the property from any content pages.

Khalid Rahaman
+1  A: 

With ASP.NET, you are not really supposed to know or care about HTTP requests and posted form values. The framework encapsulates these things, allowing you to deal with your TextBox as a component in a Windows GUI environment.

In your code, you are able to get and set its value using its Text property:

string whatsInThatBox = myTextBox.Text;
myTextBoxText = "Now, let's write something else here...";

Usually, you should only care about the cryptic names and ids of the rendered <input> elements if you need to add client side code referencing the elements.

Jørn Schou-Rode