[ASP. NET Notes] 10. Custom Controls

Source: Internet
Author: User
1: why Web user controls?
2: How to Create web user controls
3: Add properties to Web User Controls
4: add methods to Web User Controls
5. Add events to Web User Controls
6. Web user control application example

Why do we need to customize Web controls?
Combine existing Web controls to implement page Code . We can
Make a web control in some areas.
Any part

How to customize Web controls
Add --> Web user control. The extension is *. ascx.

Add attributes to custom controls
1) the method for adding attributes is the same as that for winform.
2) If this property is dependent on a Web Control of the custom control, you can directly
Add attributes, but if it is a simple attribute, you must use the session object to save
For example:
Public String myproperty
{
Get {return session ["myproperty"]. tostring ();}
Set {session ["myproperty"] = value ;}
}

Add methods to custom controls
Same as winform

Add events to custom controls
A: On the Custom User Control page
1) declare event pointer
Public event eventhandler onclick;
(Event handle type) (pointer name)
In the future, this pointer points to the same type as eventhandler.
Event Process
2) trigger event:
If (this. onclick! = NULL) // indicates that if the pointer has a specific point
{
This. onclick (sender, e); the pointer is used to execute the event it points.
}
B: write in the page_load event of the page using this control:
This. myusercontroll1.onclick + = new eventhandler (longinbutton_onclick );
Note that the corresponding code is automatically generated using the tab key.
Example:
1) create a web user control in the logon box, add the username and password attributes, and add the event that clicks the logon button.
2) create a web control specifically used to display some objects, like a list, with hyperlinks
Like displaying news
                                                                           

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.