Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My company's web application projects are heavily based on user controls. One common case is like we want to reuse the UI visual elements but note the packaged original logic .

I have read some very smelly code in some user controls code behinde.

if(WebConfig.CurrentProject == "xxx")
{
    //do xxx's logic
    ddlPostalCode.DataSouce= this.addressManager.getUSAPostal();
}
else
{
    //do yyy's logic
    ddlPostalCode.DataSouce= this.addressManager.getUKPostal();
}

//Or use configuration 
if(config.enableAlpha)
{
   //Do alpha's logic
    ddlPostalCode.DataSouce= this.addressManager.getAlphaPostalCode();
}

And these logics normally involving manipulating server controls .

So I am thinking, if we just want to reuse the visual element and parts of its logic. Why don't we make the server and its event handler public ?

//In xxx.ascx.cs
public EventHandler AddressControl_Init;

protected override void OnInit(EventArgs e)
{
     if(this.AddressControl_Init!= null)
     {
         this.AddressControl_Init(sender, e);
     }
     else
     {
        //Default logic.
     }
}

//In xxx.ascx.designer.cs
public  public global::XYZ.Controls.Basic.XYZDropDownList ddlPostalCode;

//In the code behinde of the page using this address ascx.
//HostingPage.aspx.cs
protected override void OnPreInit(EventArgs e)
{
    this.AddressControl.AddressControl_Init += new EventHandler(USAAddressControl_Init);
}

void USAAddressControl_Init(object sender, EventArgs e)
{
    //Now i can use any business manager class here to populate the source data.
    this.AddressControl.ddlPostalCode.DataSource = Anymanager.GetMarsCode();
    this.AddressControl.ddlPostalCode.DataBind();
}

This is a very simple example, but it also can be a very flexsiable solution. It decouple the logic behinde the user control. Let the user, the page to deside the control's logic.

Is there any better elegent ways to solve my problem?

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

Is there any better elegent ways to solve my problem?

You are going down a horrible path messing with all these user controls. ASP.NET MVC is much better for dealing with reusable visual parts, and everything happens on the request level so you don't have to worry about horrible event ordering.

Can't use MVC? Forget the complex events of user controls entirely. Put no code in user controls. If you need the data from a textbox in a control for example, pull it directly from the post variables (wrap this in a class if you need to)

Make your goal to deal with requests in a SINGLE, centralized point. It's the MVC way, it's the MVP way, it's the MVVM way.

Need ajax? scrap ajax controls and hit a web service; throw away the ajax toolkit.

I actually wish I had an old crufty webforms app that I could re-work this on. I would completely bail on the failure that is asp.net lifecycle

share|improve this answer
add comment

It looks like all you need is to access your controls in usercontrol directly in HostingPage.aspx.cs. You can make them public. Just find the declaration and change it to public. Then you can access them directly in Init method of HostingPage, you don't need event for that.

Nicer solution would be to create public properties in your usercontrol and use them to pass data from HostingPage to usercontrol and then do your binding bind there.

I don't think using usercontrols is bad, they can be very powerful tool. I suggest you to define interfaces and then access usercontrols trough them. In this case you can also load them dinamically with no problem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.