Can ASP.Net form have method=get or post attribute

2019-07-04 17:46发布

问题:

I am new to asp.net.

My question is, can a ASP.net form with runat="server", have a method attribute in it?

For example:

<form id="form1" runat="server" method="get">
.......
</form>

Is this possible?

回答1:

Thanks for your answers.

I would like to share some points which I found.

By default the form with runat="server", will have method="post".

But when we request a page for the first time, (i.e) request is not a postback, the method="get".

And it becomes method="post",while postback.

I checked this by placing a piece of code in code behind:

In Page_Load():

if(Request.RequestType=="GET")
{
Response.Write("Request is a GET type");
}
else if(Request.RequestType=="POST")
{
Response.Write("Request is a POST type");
}

By default, the output

  1. For the first request of that page: Request is a GET type

  2. In postback: Request is a POST type

If i give the following code in the WebForm1.aspx

<form id="form1" runat="server" method="get">

For this, the output will be:

  1. For the first request of that page: Request is a GET type

  2. In postback: Request is a GET type

This is what I found.

Thank you very much for your responses.



回答2:

yes you can try like below.

design part you can design a form like :

<form id="form1" runat="server" method="post">
    <input type="radio" name="Gender" value="male" id="test" checked="checked" />
    male
    <input type="radio" name="Gender" value="female" />female
    <input type="submit" value="test" />
    <asp:Button ID="btn" runat="server" Text="value" />
</form>

and how to get value from the form :

if (Request.Form["Gender"] != null)
 {
     string selectedGender = Request.Form["Gender"].ToString();
 }

with this way you can get the value from form in asp.net.

i hope it will help you.



回答3:

Yes,You can use use Method attribute..

The default will be Method="Post"

The form is always submitted to the page itself. If you specify an action attribute, it is ignored. If you omit the method attribute, it will be set to method="post" by default. Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET.

If you select view source in an .aspx page containing a form containg these properties...

Please refer : http://www.w3schools.com/aspnet/aspnet_forms.asp