Acceder a un formulario en 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#.

preguntado el 08 de noviembre de 11 a las 19:11

By the way, I KNOW it's Visual Basic. That is why I posted it. I keep getting told by different sites to use that, but my site is in C#. -

3 Respuestas

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.

respondido 08 nov., 11:23

Probably needs a runat="server" in there, as well. - 3 Dave

@DavidLively correct! thanks for the correction. Updated my markup. - Ícaro

Your method should be POST.

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

respondido 08 nov., 11:23

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

respondido 08 nov., 11:23

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.