views:

945

answers:

3

I'm having a problem setting up an event on a form. Here's the setup:

I was given a page with two controls, two different versions of a form for the end-user to fill out- standard and advanced. The two forms have different code and javascript, so the original dev put them in separate web user controls. Aside from the controls is a LinkButton that switches to Advanced mode.

<uc1:Standard runat="server" ID="StandardForm" />
<uc1:Advanced runat="server" ID="AdvancedForm" />
<asp:LinkButton runat="server" ID="lnkAdvanced" Text="Go To Advanced" OnClick="lnkAdvanced_Click" />

lnkAdvanced_Click just takes all the info currently entered to the advanced and flips the Visible.

My problem is that one of the bosses wants the 'Go to Advanced' button inside the standard form, but the .Visible code is on the page. So I thought it could be done using an event, but it doesn't seem to be working.

I tried to set up the event like this:

public event EventHandler AdvanceClick;

protected void lnkAdvanced_Click(object sender, EventArgs e) {
    AdvanceClick(sender, e);
}

And when that didn't work, I tried to set up a delegate:

public delegate void AdvancedEventHandler(object sender, EventArgs e);
public event AdvancedEventHandler AdvanceClick;

When I moved the button to the standard form, I expected to be able to type something like:

StandardForm.AdvanceClick += new AdvancedEventHandler(GoToAdvanced);

But it doesn't seem to recognize any events within the control! I get an error: "Standard does not contain a definition for 'AdvanceClick' and no extension method 'AdvanceClick accepting a first argument of type 'Standard' could be found" It finds the other properties just fine, am I going about this the wrong way?

A: 

You shouldn't need the delegate because you've created a standard event.

Try in your form load or thereabouts:

StandardForm.AdvanceClick += new EventHandler(GoToAdvanced);

Then somewhere on the page that hosts the 2 user controls

protected void GoToAdvanced(object sender, EventArgs e)
{
    //Code that was previously in lnkAdvanced_Click on page.
}

Edit:

It does sound like the setup is wrong.

Can you post the markup for the Host page (at this point we are assuming it is simply the 2 user controls).

Then we are also assuming that the AdvanceClick event is declared in the Standard UC but the error message would indicate that it doesn't.. and the lnkAdvanced_Click method is in the Standard UC?

Then we are assuming the code that is attempting to attach to the custom event is declared in the Host page.

If you could confirm or deny the assumptions i'm sure we could get this cleared up.

Quintin Robinson
+1  A: 
// in your Standard user control
public event EventHandler AdvancedClick;

private void lbtnAdvanced_Click(object sender, EventArgs e)
{
    OnAdvancedClick(e);
}

protected void OnAdvancedClick(EventArgs e)
{
    if (AdvancedClick != null)
        AdvancedClick(this, e);
}

// on your page
StandardForm.AdvancedClick += new EventHandler(StandardForm_AdvancedClick);

private void StandardForm_AdvancedClick(object sender, EventArgs e)
{
    // toggle logic here
}
ob
This approach would couple the standard and advanced forms together instead of delegating the logic to the host page. As long as the coupling of the controls isn't a problem then ok, but I would generally avoid this when not necessary.
Quintin Robinson
agreed, i didn't pay attention to the fact that his toggle logic was on the page. i've updated my answer accordingly.
ob
A: 

If the standard form has the "Switch to advanced" button. Then, clearly, it has to know about the Advanced form and thus they seem to be pretty tightly coupled. If this is the case, it seems to me that you might as well just have the advanced form as a child of the standard form then... or better yet, merge them into one control.

If you don't like these options you might want to create a third controls which hosts the button and the two forms, along with the logic to move data between them and toggle their visibility.

I personally recommend the single control option. Having tighly coupled controls usually just leads to confusion down the road. You could loosen up the dependency in various ways, but think hard about it before you do so.

In the legacy project I currently work on we have a bunch of examples such as serach forms and search results being split up into multiple controls, but then in the end needing each others instances to function properly. As I said earlier, I wont reccomend this path.

JohannesH
These controls have no need to be tightly coupled, if the logic is delegated to the host page then the "advanced mode" can be switched to anything at anytime without modifying the standard control because only the host needs to know what to do. That's the whole advantage to exposing the event.
Quintin Robinson
How many uses do you really think a button called "Go to advanced" will have? I mean, it's not like its ever going to switch to anything but the advanced form. If it were, I don't think the button has anything to do on the standard form it should reside in the hosting page/control.
JohannesH
This setup just smells funny. I don't think it's nearly as complicated as everybody is trying to make it. I wish I knew more about the controls requirements, use cases and their relation to eachother. I just guess that the standard and advanced controls aren't meant to be used one at a time. ;)
JohannesH
Agreed. Something's gotta be wrong with the setup, and rather than spending time figuring it out, I just took the forms out of controls and into panels as a "Whatever Works" move. Thanks for everyone's help! :)
Coronus