Birdman
Birdman

Reputation: 5414

How do I retrieve HTML POST data for manipulation in ASP.NET WebForms?

I have the following html:

<html>
    <body>

        <form runat="server">
        Name: <input type="text" name="name" />
        <br />
        <input type="submit" name="submit" />
        </form>

    </body>
</html>

How do I retrieve the value in the "name" textbox posted back to the webserver to manipulate in ASP.NET WebForms?

(I know about the ASP.NET built-in controls and the possibilities with them, but I am looking for a "clean" solution without the use of built-in ASP.NET controls)

Upvotes: 12

Views: 13498

Answers (4)

Adam Rackis
Adam Rackis

Reputation: 83358

If you can't, or don't want to use asp.net textboxes, then you can retrieve the name of a regular html textbox like this:

string nameTextPosted = Request.Form["name"];

Just note that textboxes created in this manner will not automatically persist their values across postbacks like asp.net textboxes will.

Upvotes: 19

Mubarek
Mubarek

Reputation: 2689

ASP.net includes Html server controls for backward compatibility for just someone like you fond of html. make your html tags server controls by adding the runat="server" and id properties and you are able to access them inside your server side code with their id.

 <form runat="server">
    Name: <input type="text" name="name" id="name" runat="server" />
    <br />
    <input type="submit" name="submit" id="name1" runat="server" />
    </form>

Now after this you can control their behavior:

name.Value="Hellow World !"

Upvotes: 2

Dewasish Mitruka
Dewasish Mitruka

Reputation: 2896

You have to add id and runat="server" in each control. like this :

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

Its better to use asp:TextBox like this :

<asp:TextBox ID="name" runat="server"></asp:TextBox>

Upvotes: 0

George Johnston
George Johnston

Reputation: 32258

Simplest solution would be to turn it into a server-side component and access it by it's name. e.g.

<asp:TextBox Id="Name" runat="server"></asp:TextBox>

...

string name = Name.Text;

Unless you have other reasons not to use a component, you'd only be making things much more difficult on your part for no justification.

Upvotes: 5

Related Questions