What are the important points to be considered before making a user control?
Suppose I want to make user control which will use in开发者_如何转开发 other pages. What are the important points to be considered before making a user control in ASP.Net using C#?
User controls are one of ASP.NET methods to increase reusability of code, implement encapsulation and reduce maintenance. User control is similar to web page. Both web pages and user controls contain HTML elements and markup for web controls. Some tags, like <html>
, <head>
or <form>
cannot be used in web user controls.
Here is 2 links where you will get imp points as well as the source code.I hope it helps you.
UserControl
UserControl
As per my understanding, before planning for a user control, we need to think about :
1) Is it useful at multiple places, if not immediately, even in future development.
2) If the user control is being used on the same page twice or more (eg. user control for Address), then there will be a chance of Control Id's being repeated. For which you need to plan, how you can handle that scenario(Eg. Prefixing all the controls inside user control or reading the control based on the div id etc.)
3) For a single text box kind of requirements don't go aggressively for user control (until it is really needed) because of the infrastructure needed for the user control.
I hope these points could be helpful for you.
Not sure if you are asking for reasons for creating user control or guidelines for creating one. I am assuming later and in such case, guidelines will be in general same as those for any component development.
User-control is re-usable piece of code - so you should design its interface (contract) carefully based on intended usage. For example, how consumer will pass the data to control (either property setter or calling some method). Control should notify consumer typically using events etc.
Consider user-control as a black box i.e. it should not expose its constituent controls and fields to consumer. Its better have properties or methods to access such internal state. Again property should not expose the internal control as is - for example, if user control is capturing address then it make sense to expose property that will get/set Address object instead of giving ref to individual address field controls.
Try to give short names for controls within user control. User control itself is a naming container so names should be unique over a page. Giving log descriptive name some times result in creation of long ids increasing markup size. For example, if Address user control has text box named StreetNumber and Page creates instance of user controls and names it as CustomerAddress then actual field will get name/id such as CustomerAddress_StreetNumber. I am not saying that one should use cryptic names within user control but be aware of this when creating and using user control.
精彩评论