4
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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