Best Practices for making user controls in asp.net?

别说谁变了你拦得住时间么 提交于 2020-01-15 07:20:30

问题


What are the best practices in using user controls ? my user controls are usually forms that submit data on server and they are made to reuse in pages.

Where css styles should be placed inside user control ? or in aspx page and these styles should be in page or external css ?

Similarly success or faulure message is usually a part of page. But in using user controls should user contorl tell the page about result or message lable should be inside the control ?

Any thing else to consider while using user controls ?


回答1:


  1. The page is the controller and it's doing the saving/editing, therefore it also should have the label which shows result.

  2. Page and UserControls should communicate in the following way

    • Page -> UC via Properties and Functions
    • UC -> Page via Events

Here are some more infos: http://www.codeproject.com/KB/user-controls/Page_UserControl.aspx#3




回答2:


Similarly success or faulure message is usually a part of page

True, it's common to have an error message in the same place on all related pages, possibly by putting it on a Master page.

A common approach is to implement a base class for your UserControls and Pages which implements the plumbing for common stuff such as display of error messages. Something along the lines of the following rough skeleton:

public class MyUserControlBase : UserControl
{
    ...
    protected MyPageBase MyPageBase
    {
        get 
        { 
            PageBase pageBase = Page as PageBase;
            if (pageBase == null) throw new InvalidOperationException(
                    "This UC must be on a Page derived from MyPageBase");
            return pageBase;
        }
    }
    ...
}

public class MyPageBase : Page
{
    ...
    public void ShowError(string errorMessage)
    {
        MyMaster master = this.Master as MyMaster;
        if (master != null) master.ShowError(errorMessage);
    }
    ...
}



回答3:


First of all, it is generally considered best practice to use external css files for your styling. This way you have your styles defined in one central place, so they can be re-used across multiple pages and you can modify your styles without having to modify all your pages/controls. However, if you only have a single page, you might as well put your CSS code in the page header.

Secondly, if you are referring to form validation, I would implement this in your user control. This way, if you are going to re-use your control a lot, you don't need to rewrite your validation everytime.

If you want to pass information from the page to the user control, you simple need to expose the appropriate properties or methods to the page by making them public or internal. However passing information from the user control is a little harder. One way to do is by passing a delegate from the page to the control, or by raising event inside the control and have the page listen to this. Either way, it is considerd bad practice to have the control know about any implementation details page, since this would prevent any re-use.

So, if you want to show error messages fromyour control on the main page, I would either expose this message as a property or through an event to which the page can listen.



来源:https://stackoverflow.com/questions/5509776/best-practices-for-making-user-controls-in-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!