Asp.net subform refresh parent form (delegate) method

Source: Internet
Author: User

Child form ff = new child form

The code is as follows: Copy code
Ff. showdialog ();
If (subform. DialogResult = DialogResult. OK)
{
Datashow ();
}

This method can be written in the button_click event of the parent form.

Another method is to use the asp.net delegate (through simulation, ASP. NET can execute code and access resources in an authenticated and authorized user context, but can only run ASP.. NET server. To access resources on other computers on behalf of a simulated user, you need an authentication delegate (delegated for short ). You can think of delegation as a more powerful form of simulation because it enables simulation through the network. Let's look at an instance.

Let's take a look at the simple delegation

The code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

Namespace TestMouseMove
{
Public delegate void SetVisiableHandler ();

Public partial class Form2: Form
    {
Public Form2 ()
        {
InitializeComponent ();
        }
Private void button#click (object sender, EventArgs e)
        {
Form3 frm = new Form3 (new SetVisiableHandler (SetVisiable ));
Frm. Show ();
        }

Private void SetVisiable ()
        {
SetVisiable (this. label1 ,! This. label1.Visible );
        }

Private void SetVisiable (Control control, bool visiable)
        {
If (this. Controls. Contains (control ))
            {
Control. Visible = visiable;
            }
        }

    }
}

Form3

The code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

Namespace TestMouseMove
{
Public partial class Form3: Form
    {
Private SetVisiableHandler m_setVisible;

Public Form3 (SetVisiableHandler setvisible)
        {
InitializeComponent ();
This. m_setVisible = setvisible;
        }
Private void btnVisible_Click (object sender, EventArgs e)
        {
If (this. m_setVisible! = Null)
            {
This. m_setVisible ();
            }
        }

    }
}

About delegation
1. The namespace defines a delegate void CustomEventHandler ()

2. The user control defines the event public event CustomEventHandler CutomEvent (), and then calls CutomEvent () where appropriate ();

3. Someone else registers his method on the page: UserControlInstance. CustomEvent (CustomMethod); CustomMethod is his own method (re-bind data and so on .), The signature must be consistent with the delegate.

(Note: the registration method must be executed every time pageload is executed)

If you do not understand delegation, refer to http://msdn.microsoft.com/zh-cn/library/aa291350 (v = vs.71). aspx

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.