ASP. NET Web Programming

Source: Internet
Author: User

runat= "Server" directly back to the server, processing the data, and the data encrypted after the hidden property of the input control insert, to achieve the state of the form save

ruant= "Server" means that the control is running on the server side, saying that the simple point is that you can refer to this control in the. CS background code.

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Registration.aspx.cs" inherits= " Eventregistrationweb.registration "%>
Defines the programming languages and classes to use. The property autoeventwireup= "True" indicates that the page's event handlers are automatically linked to the specific method name. inherits= "eventregistrationweb.registration" means that dynamically generated classes in the ASPX file derive from the base class registration,
This base class is located in the code-behind file Registration.aspx.cs defined with the Codebehind property.

There are also HTML elements with runat= "server" attributes, such as form elements. by runat= the "server" feature,
The ASP. NET server controls are associated with HTML tags. This control can be used to write server-side code. In the form
The element is followed by an object of type System.Web.UI.HtmlControls.HtmlForm, which has an object defined with the id attribute
The variable name Form1. Form1 can be used to invoke methods and properties of the HtmlForm class.
The HtmlForm object creates a form tag that is sent to the client.

<form id= "Form1" runat= "Server" >

string selectedevent = Dropdownlistevents.selectedvalue;

The SelectedValue property returns the current selection

Try
{
DropDownList dropdownlistevents =
(DropDownList) Previouspage.findcontrol ("dropdownlistevents");
string selectedevent = Dropdownlistevents.selectedvalue;
String firstName = ((TextBox) Previouspage.findcontrol (
"Textfirstname")). Text;
String lastName = ((TextBox) Previouspage.findcontrol (
"Textlastname")). Text;
string email = (TextBox) Previouspage.findcontrol (
"TextEmail")). Text;
Labelresult.text = String.Format ("{0} {1} selected the event {2}",
FirstName, LastName, selectedevent);
}
Catch
{
Labelresult.text = "The originating page must contain" +
"Textfirstname, Textlastname, TextEmail controls";

Set the PostBackUrl property of the Submit button on the Registration.aspx page to resultspage.aspx.

Echo

The browser sends all the data from the form on the first page to a new page, but on the newly requested page, you need to
Gets data from the control in the previous page definition. To access the controls in the previous page, the page class defines the property previouspage.
PreviousPage returns a Page object that can be accessed using the FindControl () method. FindControl ()
is defined to return a control object, you must convert the type of the return value to the type of control you are searching for.

DropDownList dropdownlistevents =
((DropDownList) Previouspage.findcontrol ("Dropdownlistevents")). Text;

Create a strongly typed object

Try it: Create a strongly typed previouspage
(1) Select "Project?" Add Class "(Project? Add new Class), adding the project to the
Registrationinfo.
(2) Implement the Registrationinfo class as follows:
public class Registrationinfo
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
public string Selectedevent {get; set;}
}
(3) Add a public property registrationinfo to the registration class in the Registration.aspx.cs file:
Public Registrationinfo Registrationinfo
{
Get
{
return new Registrationinfo
{
FirstName = Textfirstname.text,
LastName = Textlastname.text,
Email = Textemail.text,
Selectedevent = Dropdownlistevents.selectedvalue
};
}
}
(4) Add the PreviousPageType Directive below the page directive of the Resultpage.aspx file:

<%@ page language= "C #" autoeventwireup= "true" codebehind= "ResultsPage.aspx.cs"
inherits= "Eventregistrationweb.resultspage"%>
<%@ previouspagetype virtualpath= "~/registration.aspx"%>


(5) in the Page_Load () method of the Resultspage class, the code can be simplified to:

protected void Page_Load (object sender, EventArgs e)
{
Try
{
Registrationinfo ri = previouspage.registrationinfo;
Labelresult.text = String.Format ("{0} {1} selected the event {2}",
Ri. FirstName, RI. LastName, RI. Selectedevent);
}
Catch
{
Labelresult.text = "The originating page must contain" +
"Textfirstname, Textlastname, TextEmail controls";

}

}


The PreviousPageType directive creates a property of type previouspage that returns the type associated with the directive. In
In its implementation code, the PreviousPage property of the base class is called, as shown in the following code:
Public new Eventregistrationweb.default PreviousPage {
get {
Return ((Eventregistrationweb.default) (base. PreviousPage));
}
}
There is no virtualpath attribute that uses the PreviousPageType directive to define the type of the previous page, and the
TypeName characteristics. You can use this feature if you have multiple pages in front of you. At this point, you need to define all of the previous pages
A base class and assigns the base class to the TypeName attribute.

ASP. NET Web Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.