In asp.net, values are transferred between the child window and the parent window by Delegate and event.

Source: Internet
Author: User

This article introduces asp.net's implementation of passing values between the child window and the parent window through delegation and events. There are two methods, one is to pass the parent window to the Child window, set the attribute or resource of the parent window to public, and use the delegate event for handling. For more information, see.

The following method uses a delegate event to implement the above functions:

Take logon as an example. The subform AccountWindow:

① Define delegation:

The Code is as follows: Copy code

Public delegate void LoginSuccessedDelegate (object sender, LoginSuccessedEventArgs e );
Public event LoginSuccessedDelegate LoginSuccessedEvent;

② Event pass value:

The Code is as follows: Copy code

Private void LoginButton_Click (object sender, RoutedEventArgs e)
{
This. Close ();
LoginSuccessedEventArgs loginAccount = new LoginSuccessedEventArgs ();
LoginAccount. Account = "123" + DateTime. Now. Second. ToString ();
LoginSuccessedEvent (sender, loginAccount );
}
// Define the data type of the passed Value
Public class LoginSuccessedEventArgs: EventArgs
{
Public string Account {get; set ;}
}

Parent form:

① Trigger a logon event

The Code is as follows: Copy code

Private void Login_Click (object sender, RoutedEventArgs e)
{
AccountWindow accountWindow = new AccountWindow (this );

// Register an event
AccountWindow. LoginSuccessedEvent + = new AccountWindow. LoginSuccessedDelegate (accountWindow_LoginSuccessedEvent );
AccountWindow. ShowDialog ();
}

② Handle the return value of a successful logon event (in this case, we can obtain the return value for logon and operate the attributes and resources of the parent form based on the return value, saving the need to pass the parent form to the child form)

The Code is as follows: Copy code

Void accountWindow_LoginSuccessedEvent (object sender, AccountWindow. LoginSuccessedEventArgs e)
{
UserInfo. Content = "Current Account:" + e. Account;
}

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.