Getting variables from a post or get?

Sorry for the newbie post, but i am from a PHP world. Im now learning ASP.NET.
Im having a bit of problem figuring out how to get/post a variable on an input field. All i want to do is, get the value from an input form and display it again on the page.

Here's my code..


form id="form1" runat="server" acceptcharset="utf-8">

Your name:

<input id="name" name="name" type="text" />

<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="Default.aspx" />



All i want is to get the value of name and display it on the page it self. How can i do that?

On PHP, i would normally do is..

$name = $_POST["name"];
echo $name;

Please help, thanks.

I am a PHP Developer, Now learning ASP.NET. I want to be an ASP.NET Developer soon!
12/15/2006 4:03:03 AM 93655 articles. 6 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 3

you would look at the request string


String name = Request["name"];


Peter Kellner and blogging at
12/15/2006 4:32:31 AM

Either replace the Input with an ASP:Texbox whihc generates an HTML Input Element or add the runat attribute to the existing input element to instantiat an HTMLServer control.

<input id="name" name="name" type="text" runat="server"/>


asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>

then access in your coed behind with the ID


"Textbox text is " & Me.TextBox1.Text)




For the benefit of all users please mark any post answers as appropriate.
12/15/2006 4:32:41 AM

You would use Request.Form["name"] or even Request["name"] to get the field, assuming that you're working in C# (use () for indexers in Visual Basic).

You might also want to consider replacing your HTML only <input> element with an <asp:TextBox>, as this will

a. Enable the control to remember its value between postbacks, and

b. Mean that you can access the content of the TextBox simply by using it's Text property (e.g.) name.Text.

Finally, the equivalent to echo $name is Response.Write() in ASP.NET. However, this is a really, really bad thing to do, due to the nature of the page event cycle that happens on the server. In ASP.NET, pages prepare themselves for rendering (by handling events such as Init, Load, button Click events, and so on), and then the page is rendered in the Render event, which occurs towards the end of the event cycle.

The page maintains a tree of controls, and calls there Render methods in turn.

Thus, using Response.Write() in, say, a Page's Load event handler will cause the output to be generated before the page really renders itself, which means that your output will appear before such exciting items as the DOCTYPE declaration. And that's bad.

Therefore, it would be much better to use a Label or other display control to display the output (in the case of the Label, you could simply set its Text property to the Text property of an ASP.NET TextBox.

Understanding the Page event cycle is absolutely critical to being able to write ASP.NET pages. In fact, it's arguably the number one thing to get your head around, so if your moving from PHP, I'd be tempted to read Fritz Onion's excellent ASP.NET books (either for ASP.NET 1 or ASP.NET 2, depending on which version you're using), because it covers how ASP.NET processes requests better than any other book I've seen.




12/15/2006 4:35:44 AM


try this

  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
           <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" PostBackUrl="~/Default2.aspx" />

add <%@ PreviousPageType VirtualPath ="~/Default.aspx" %> to Default2

public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
       TextBox tb=(TextBox)PreviousPage.FindControl("TextBox1");
       Response.Write (tb.Text);


12/15/2006 4:46:31 AM


I think that implementing a cross page postback is more than over the top for simply displaying the value of an input text on a page, and wouldn't recommend it at all in this case. The earlier posts already cover how to post back and read the value within a form, and that should more than suffice.

Secondly, using FindControl is very poor programming practice and should be avoided wherever possible. It introduces a level of coupling between the classes (or pages, if you must) which really should not be allowed to propagate in code. To be honest, the <%@ PreviousPageType %> directive, when used with a VirtualPath attribute, is also a pretty ropey idea, and again should be avoided where possible.


12/15/2006 5:13:55 AM

hi DMW,

thank you so much for your kind advice, it does make senseBig Smile

12/15/2006 5:19:25 AM

Similar Artilces:

Hi, I'm at a loss to explain this... I have an ASPX page that I can't always navigate to. When I developed the app (it's Starter Kit Portal-based), I did it on my LAN and have had no trouble accessing the page. However, upon deployment, I see the following behavior: 1. POST request goes out looking for MyForm.aspx 2. GET request for MyForm.aspx 3. GET request for MyForm.aspx 4. ... X. GET request Sometimes, the page finally loads. Other times, it just ends up blank. Many times, it doesn't load at ALL the first POST - I just get a "The pa...

getting start with and crystal repots(web form)
I use Crystal Report9 in Visual Studio.NET 2003..... Example at Northwind database I want a application that.....a form include a combobox that contain CustomerID.....If I choose one CustomerID and click OK button....a Report will display information about this Customer......I think that must use to do that ???? (How do I start to coding --please step by step or sample) please help me !!! Thank you so much!!! very good I have Dropdownlist when I choose the 'month'--> the all data of this field will show I use this Private Sub ddlMonth_Sel...

VB .net Web Form will not get contents of Text Box
I have created a web form with text boxes that are bound to a DataSet/DataView.  I have created button to navigate up and down the recordset.  That all works fine. I need to have the ability to edit/add text in any of the boxes and then when clicking "Save" button, it would build an UPDATE query to send to the database. When the Save button is clicked, I have coded to DIM a string var (strSQL) to use to build the SQL command.  For some reason, it will only get the text from a text box if the data was pulled the database.  If I add/edit data, then the SQL command will ha...

IN VS.Net how can i get a web form to scroll (design mode)
In design mode in VS.Net, there is a scroll bar next to the web form, but it is grayed out.  I can not figure out how to enable it.  I want to design a web form that is larger in size than the surface area shown in teh deisgn window.  Can someone help out with this?Thank you. VS design surface shows how the page is going to be rendered in the browser. Therefore, there is no 'virtual' large size. In the browser scroll bar appears if the browser window is smaller than the page content. You can make scroll bar to appear by enclosing page content in a large container e...

getting values from a web form to windows form
Hot Requirement ..... Is there any possible of getting values from a web form to windows form ????? Yes One way i to use the WebRequest and WebResponse classes of the System.Net namespace. For example:  WebRequest request = WebRequest.Create("URL of the web page); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string content = reader.ReadLine(); ....    Reza Nassabehwww.professionalcsharp.comDon't forget to click "Mark as Answer" on the post that helped you. That way fut...

Do not get expected WEb page on secondary
netware 6.5 sp2 Have setup a web page on a seconary IP address Load appache with the following command line LOAD ADDRESS SPACE = CSMI APACHE2 -f SYS:APACHE2\CSMI\SERVER\HTTPD.CONF Where HTTPD.CONF is set up to Listen which is a secondary ip address A display secondary ipaddress at the server lists the seconary ipaddress I can ping the secondary ip address from the client in Remote manager using IP Address management the secondary IP addresses are not listed When I attempt to brose to the HTTP:// address I do not see the web page I...

Why do I get a Cannot Get Web Application Service
I dont know what happen to my web site in IIS. but whenever I configure a data source I always get a "Cannot Get web application service" Please help. thanks!I don't know .NET! Why I'm here??? Hi, adsmag: My solution was to save my work, shut VWD, reopen it, reload my work - and hey presto it workedBest Regards,__________________________________________________Sincerely,Rex LinMicrosoft Online Community SupportThis posting is provided "AS IS" with on warranties, and confers no rights. Yep! Got it! thanks!I don't know .NET! Why I'm here??? ...

how i can get the one form variable values in other forms?????
iam going to pass values of one form to another formlike asfollows.. f1=CType(Context.Handler ,f1);here f1 is form object this will be used in some other form.iam try ing to use to global variables of first form in to second form.while compilation it show s an error like as follws:"The name 'CType' does not exist in the class or namespace 'PassingValuesForms.Form2'can you giv aslo pls..thanks in advance..ramsCheersRam MCP CType is for the VB.NET language exclusively. By adding a semicolon to your code, I'm guessing you are using C#.CType provides VB users with typecasting. In C#, the lang...

Post and Get variables
How could I possibly get post and get variables in the page preinit state ? In Page_Load method I don't have even items on this.  .. cant access form elements... :S I don't know if they are available at preinit, but you could get them using Request collections, which uses a string key (id name).Brian"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4). Found this:"Trust in the Lord and do what is go...

How to get a variable from a form...?
Hi all, Would anyone be kind enough to show me how I can get the variable “Name” from the form and print it on the screen? Using functions and not asp that is... <%@ Page Language="c#" %> <script runat="server"> protected override void Render(HtmlTextWriter writer) { writer.WriteFullBeginTag("html"); writer.WriteLine(); writer.WriteFullBeginTag("body"); //how can I print the variable "Name" on the screen in the browser writer.WriteLine("<form a...

Getting error in Net::SFTP with get function
------=_Part_7028_3315294.1193837808284 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi All, I have the following code as part of my program; print "---=========> $file, $localFile\n"; $sftp -> get( $file, $localFile ) || warn("errrrr0rrrrr _---> $!".$sftp->status."\n");^M Even though it has downloaded the file properly, it is giving the following message: ---=========> /trading/tiscrdenhlist10310000.txt.old, /export/home/crdadm/rajeev/tiscrdenhlist103...

Getting the Selected Item of dropdownlist inside Web User Control on a Web Form
Ive got a dropdownlist control inside a web user control called Entity.ascx. This web user control is on a webform.aspx. In Entity.ascx class I created a SelectedEntity property that returns the selected item in the dropdownlist. Inside webform.aspx when btnGo is pushed I set a string and pass it in the url. I am trying to get the entity they selected via the property (SelectedEntity) inside Entity.ascx. As the SelectedEntity property tries to return the selected item in the dropdownlist I get this error: System.NullReferenceException: Object reference not set to an instance...

Could PB .NET Web Form POST security Information to others web page??
This is a multi-part message in MIME format. ------=_NextPart_000_00F9_01C9C4D5.8FB45C90 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: quoted-printable Hi,=20 Have anyone came across the requesion to POST security informations from = PB .NET web form web page to other web site?? I have tried many datys, Could any kind man give me some suggessions?? thanks & Best regards Leon ------=_NextPart_000_00F9_01C9C4D5.8FB45C90 Content-Type: text/html; charset="big5" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML P...

Get message that mozilla has to close. Get booted out of web site. Get message from Mozilla to report problem. Problem reporting will not work. Eventually I delete the report message.
Name: Linnea Hagberg Email: lhagbergatsmithcovedotcom Product: Mozilla Suite Summary: Get message that mozilla has to close. Get booted out of web site. Get message from Mozilla to report problem. Problem reporting will not work. Eventually I delete the report message. Comments: See above. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7 ...

Web resources about - Getting variables from a post or get? -

Variable - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Westpac interest rate rise is one of many variable loan rate rises
WESTPAC is not alone in hitting its owner-occupier customers with a surprise interest rate rise, as dozens of other variable rate home loans ...

Airbnb upgrades variable pricing tool for hosts
Online home rental group Airbnb is rolling out an upgraded variable pricing tool that automatically raises or cuts room rates based on supply ...

Commonwealth Bank raises variable home loan rates
Home owners' mortgage costs are set to rise by hundreds of dollars a year, after the Commonwealth Bank became the latest bank to hike interest ...

CBA lifts variable home loan rates to offset costs associated with stricter capital requirements
The Commonwealth Bank to lift its home loan interest rates independently of the Reserve Bank to partially offset costs associated with stricter ...

Washington Square Signs Spielbergs, Choi Joins Variable and More
... films and an episode of the HBO comedy series "Girls," on which he plays the role of Ray Ploshansky. New York-based creative collective Variable ...

YouTube v10.32 Tweaks Some Interface Elements And Includes Hints About Virtual Reality Content And Variable ...
Forget Update Wednesday! Monday is the new big day. Not only were we treated to the latest M preview release and the official Marshmallow name, ...

"You have to ignore many variables to think women are paid less than men. California is happy to try." ...
Writes Sarah Ketterer in The Wall Street Journal in "The ‘Wage Gap’ Myth That Won’t Die" (which you can get to without subscribing if you Google ...

Staley: 'A lot of different variables...been a hard year'
... 41° Navigation Home Giants A's Sharks Warriors Kings 49ers Raiders Quakes Insiders More Tickets Shop Watch Staley: 'A lot of different variables...been ...

Expert Reveals the Latest Variable Capacity Technology
Learn about gForce Ultra CRAC equipment, the latest variable capacity technology to lower energy usage and increase reliability.

Resources last updated: 1/24/2016 1:34:24 AM