slhsen
slhsen

Reputation: 616

How can I reach form values from Content Page in ASP .NET

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?

Upvotes: 1

Views: 1638

Answers (4)

J&#248;rn Schou-Rode
J&#248;rn Schou-Rode

Reputation: 38346

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.

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630429

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;

Upvotes: 0

Khalid Rahaman
Khalid Rahaman

Reputation: 2302

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.

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37533

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.

Upvotes: 1

Related Questions