Marcus
Marcus

Reputation: 127

Access a form in asp.NET C#

All the examples I can find are in Visual Basic, but I am using C#. I want to get the data that is in a textbox in a form. My code so far:

<form action="login.aspx" method="get">
    <p>Username: <input type="text" name="username" /></p>
    <p>Password: <input type="text" name="password" /></p>
    <input type="submit" value="Submit" />
</form>

So what could I do? Because I keep getting told to do this:

Dim UserName
UserName = Request.Form("UserName")

But it doesn't work in C#.

Upvotes: 0

Views: 707

Answers (3)

Jason
Jason

Reputation: 15931

The sample code you posted is vb.net. C# has it's own syntax and keywords.

to get the value you can use the following (not an optimal solution for webforms)

string userName = Request.Form["UserName"];

I would suggest going through some c# tutorials to get a handle on the language. Here's the first one i found http://www.csharp-station.com/Tutorial.aspx

Upvotes: 0

Icarus
Icarus

Reputation: 63970

Don't need to do that on asp.net; simply change your markup like so:

<form action="login.aspx" method="post" runat="server">
    <p>Username: <input type="text" name="username" runat="server" id="txtUsername" /></p>
    <p>Password: <input type="text" name="password" runat="server" id="txtPassword"/></p>
    <input type="submit" value="Submit" />
</form>

And on code behind:

string UserName= txtUsername.Value;

And yes, Shawn also caught a good one, you should use POST.

Upvotes: 3

Shawn Steward
Shawn Steward

Reputation: 6825

Your method should be POST.

<form action="login.aspx" method="post">

Upvotes: 2

Related Questions