ASP. NET custom control Event Response

Source: Internet
Author: User

1. Overload protected override bool onbubbleevent (Object source, eventargs ARGs)
Onbubbleevent is the event bubbling of the control to the outer control.
Certificate -------------------------------------------------------------------------------------------------------------------------------------------

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI;
Using system. componentmodel;

Public class uc_danxuan: compositecontrol
{
Private button btnsave = NULL;
Public eventhandler saveanswer; // defines this function to handle Event Response outside the control.

Protected override void createchildcontrols ()
{
Controls. Clear ();
Btnsave = new button ();
Btnsave. Text = "Click me ";
Btnsave. commandname = "Name of the event"; // This is the name of the event when the event occurs.
Btnsave. commandargument = "event parameters, as required"; // This is used to pass parameters after an event occurs.
Controls. Add (btnsave );
}

Protected override bool onbubbleevent (Object source, eventargs ARGs)
{
If (ARGs is commandeventargs)
{
Commandeventargs cmdargs = ARGs as commandeventargs;
// You can add some Code In the control.
If (saveanswer! = NULL) // if you want to process the event outside, it will be handled by the event. Program Go
Saveanswer (source, argS );
Return true;
}
Else
{
Return base. onbubbleevent (source, argS );
}

}
}
Certificate --------------------------------------------------------------------------------------------------------------------------------------

Test it:
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Uc_danxuan ucdx = new uc_danxuan ();
Ucdx. saveanswer + = new eventhandler (saveanswer); // process the event outside
Form1.controls. Add (ucdx );

}
Public void saveanswer (Object sender, eventargs ARGs) // specific processing here
{

}
}
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------

2. directly respond to internal control events
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI;
Using system. componentmodel;

Public class uc_danxuan: compositecontrol
{
Private button btnsave;
Public event eventhandler saveanswer; // leave it to the control for external processing of this event

Protected virtual void onsave (Object source, eventargs e) // The control processes this event internally.
{
If (saveanswer! = NULL)
{
Saveanswer (this, e); // internal processing is complete, and external processing is performed again
}
}

Protected override void createchildcontrols ()
{
Controls. Clear ();
Btnsave = new button ();
Btnsave. ID = "BTN ";
Btnsave. Text = "save ";
Btnsave. Click + = new eventhandler (onsave); // click the response button
This. Controls. Add (btnsave );
}

Protected override void render (htmltextwriter writer)
{
Btnsave. rendercontrol (writer );
}
}
Test code is the same as above

Certificate ----------------------------------------------------------------------------------------------------------------------------------------
3. Use client JavaScript to send back and forth?
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI;
Using system. componentmodel;

Public class uc_danxuan: compositecontrol, ipostbackeventhandler
{
Private button btnsave;
Public event eventhandler saveanswer; // leave it to the control for external processing of this event

Protected virtual void onsave (eventargs e) // The control processes this event internally.
{
If (saveanswer! = NULL)
{
Saveanswer (this, e); // internal processing is complete, and external processing is performed again
}
}

Protected override void createchildcontrols ()
{
Btnsave = new button ();
Btnsave. ID = "BTN ";
Btnsave. Text = "save ";

// Use the client JavaScript to send events back.
Btnsave. onclientclick = page. clientscript. getpostbackeventreference (this, "save ");

}
Public override void rendercontrol (htmltextwriter writer)
{
Btnsave. rendercontrol (writer );
Base. rendercontrol (writer );
}

Void ipostbackeventhandler. raisepostbackevent (string eventargument)
{
If (eventargument = "save ")
Onsave (New eventargs ());
}
}
Test code is the same as above

4. Let textbox respond to onkeydown
Certificate ----------------------------------------------------------------------------------------------------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI;
Using system. componentmodel;

Public class uc_danxuan: compositecontrol, ipostbackeventhandler
{
Private textbox tbx;

Public event eventhandler onkeydown;

Protected override void createchildcontrols ()
{
Controls. Clear ();
Tbx = new Textbox ();
Tbx. ID = "tbx ";
Tbx. Attributes. Add ("onkeydown", page. clientscript. getpostbackeventreference (this, "onkeydown "));
Controls. Add (tbx );
}

Public void raisepostbackevent (string eventargument)
{
Switch (eventargument)
{
Case "onkeydown ":
If (onkeydown! = NULL)
Onkeydown (this, eventargs. Empty );
Break;
}
}
}
Test code:
Protected void page_load (Object sender, eventargs E)
{
Uc_danxuan ucdx = new uc_danxuan ();
Ucdx. onkeydown + = new eventhandler (ucdx_onkeydown );
Form1.controls. Add (ucdx );

}

Void ucdx_onkeydown (Object sender, eventargs E)
{

}

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.