1. Asp.net form submission
Problem:
Default. aspx code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Test </title>
</Head>
<Body>
<Form id = "form1" action = "1. aspx" method = "post">
<Div>
<Input id = "txtuser" type = "text"/>
<Input id = "Submit1" type = "submit" value = "submit"/>
</Div>
</Form>
</Body>
</Html>
Target file 1. aspx code: B. Is it true that a form in asp.net can only be submitted to this page, that is, default. aspx can only be submitted to default. aspx?
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "1. aspx. cs" Inherits = "_ 1" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<%
String user = Request. Form ["txtuser"];
If (user! = "")
Response. Write (Request. Form ["txtuser"]);
Else
Response. Write ("You have not entered any information ");
%>
</Div>
</Form>
</Body>
</Html>
A. If it is run, it will appear in 1. aspx and will not be able to process submitted forms. Why ??
Solution:
A. in fact, this solution is very simple, that is, in addition to the submit button, all must be added "runat = server", this is to be able to find this control on the server side according to the Control ID, otherwise, the control cannot be found, so it will not achieve the expected results, so the following code should be changed:
<Form id = "form1" action = "1. aspx" method = "post">
<Div>
<Input id = "txtuser" type = "text" runat = "server"/>
<Input id = "Submit1" type = "submit" value = "submit"/>
</Div>
</Form>
In this way, you can easily submit the form for other pages to process it.
B. Of course not. Obviously, this idea may be caused by the following code:
Correct code:
<Form id = "form1" action = "1. aspx" method = "post">
<Div>
<Input id = "txtuser" type = "text" runat = "server"/>
<Input id = "Submit1" type = "submit" value = "submit"/>
</Div>
</Form>
Error code:
<Form id = "form1" action = "1. aspx" method = "post" runat = "server">
<Div>
<Input id = "txtuser" type = "text" runat = "server"/>
<Input id = "Submit1" type = "submit" value = "submit"/>
</Div>
</Form>
We have noticed that runat = "server" is more than the above Code. This is also the key. When runat = "server" appears, its priority level is higher than action = "1. aspx "is high, so sending back may occur. Ignore the action, so it cannot be processed.
We can remove runat = "server" to solve this problem. We can also click Modify submit without removing runat = "server, click the submit button to solve the problem.
Do not remove runat = "server ":
Change the submit control to a server control:
<Form id = "form1" action = "1. aspx" method = "post" runat = "server">
<Div>
<Input id = "txtuser" type = "text" runat = "server"/>
<Input id = "Submit1" type = "submit" value = "submit" runat = "server"/>
</Div>
</Form>
Then the control of the submit button says:
Server. Transfer ("1. aspx"); at this time, you can complete the Transfer between two pages. Note: Response. Redirect ("1. aspx") cannot be used here; otherwise, this operation cannot be completed.
Summary:
1. If runat = "server" is not added to the form, "runat = server" must be added to all controls except the submit button. The position of the submit button can be placed anywhere in the form.
2. if runat = "server" is added to form, action will be ignored at this time. If the submit button is not a Server-side control, and no server is performed. transfer navigation processing cannot be completed
2. Another method for cross-Page Submission
In ASP. NET 1. x, many friends may need to handle cross-page submission, that is, from page A can be submitted to page B, and even different Control targets have different processing pages. Developers, especially those transferred from ASP, JSP, and PHP, may have such requirements. However, unfortunately, in ASP. NET 1.x, processing such cross-page requests is very ugly and requires a lot of "skillful" processing.
In ASP. NET 2.0, cross-Page Submission has a very reasonable solution. The following is an example.
To obtain a Public Member of the Source Page, you must first obtain a strong type reference to the Source Page.
You can perform this operation in multiple ways. The first method is to include a @ PreviousPageType command on the target page, which allows you to specify the Source Page:
<% @ PreviousPageType VirtualPath = "~ /SourcePage. aspx "%>
OK. With these two simple attribute settings, you can easily obtain cross-page submission features. Of course, you can also perform more complex settings based on your own needs. For example, each Control needs to be submitted to different pages.
When this command is included, the PreviousPage attribute is forcibly typed as the class of the referenced source page. Therefore, you can directly reference public members on the Source Page. You can use the type property to directly specify the type of the Source Page, or explicitly reference the source page in the VirtualPath property to indirectly specify the type of the Source Page, as shown in this example.
The following code example shows a part of the Source Page, which contains a public property named CurrentCity, which is used to publish the value of the TextBox Control named textCity.
Visual Basic
Public ReadOnly Property CurrentCity () As String
Get
Return textCity. Text
End Get
End Property
C #
Public String CurrentCity
{
Get
{
Return textCity. Text;
}
}
Note:
The attribute created on the Source Page mainly used to send public values across pages is usually read-only. Although the Source Page can contain public read/write attributes, setting the Source Page attribute through the target page attribute does not have any effect, because this value is not retained.
If the target page contains the PreviousPageType command directed to the Source Page, you can use the following code to access the CurrentCity attribute of the Source Page.
Visual Basic
Label1.Text = PreviousPage. CurrentCity;
C #
Label1.Text = PreviousPage. CurrentCity;
Another way to obtain a strong type Reference to the Source Page is to include a @ Reference command in the target page of the Source Page, just as any type of Reference to be used on the page. In this case, you can obtain the PreviousPage attribute of the target page on the target page and forcibly convert it to the Source Page type, as shown in the following code example.
Copy code in Visual Basic
Dim sourcePage As SourcePage_aspx
SourcePage = CType (PreviousPage, SourcePage_aspx)
Label1.Text = p. CurrentCity
C # copy code
SourcePage_aspx sourcePage;
SourcePage = (SourcePage_aspx) PreviousPage;
Label1.Text = sourcePage. CurrentCity;
Check sending back on the target page
During cross-page sending back, the content of the Source Page Control is sent to the target page, and the browser performs the http post operation (not the GET operation ). However, on the target page, after sending a cross-page message, the IsPostBack attribute becomes false immediately. Although this behavior is POST, cross-page sending is not a sending back to the target page. Therefore, IsPostBack is set to false, and the target page can use its first code.
If this is useful in your application, you can determine whether the target page is running due to cross-page sending. To this end, you can test the IsCrossPagePostBack attribute referenced by the page returned by the PreviousPage attribute on the target page, as shown in the following code example.
Visual Basic
If PreviousPage IsNot Nothing Then
If PreviousPage. IsCrossPagePostBack = True Then
Label1.Text = "Cross-page post ."
End If
Else
Label1.Text = "Not a cross-page post ."
End If
C #
If (PreviousPage! = Null)
{
If (PreviousPage. IsCrossPagePostBack = true)
{
Label1.Text = "Cross-page post .";
}
}
Else
{
Label1.Text = "Not a cross-page post .";
}
Note that if the current page is not sent across pages, the PreviousPage attribute returns null (Nothing in Visual Basic ).
For more information, see How to: determine how to call ASP. NET web pages.
Cross-page transmission and Server. Transfer
PreviousPage attributes and PreviousPageType commands are useful when calling the target page: in cross-page sending (this is a client-based transmission) and when using the Transfer method (this is a server-based operation ). In the preceding two operations, the code on the target page can use the PreviousPage attribute to obtain references to the Source Page.
In the target page, it may be important to determine whether the page is sent across pages or the Server. Transfer operation call. To help you perform this operation, the Page class discloses an attribute named IsCrossPagePostBack. For more information, see How to: determine how to call an ASP. NET webpage.