views:

638

answers:

2

I am attempting to manage an ajax connection by calling a button onclick method on a separate web part in order to force the partial postback on the consumer.

Web part A (Provider) invokes the method on Web Part B (Consumer)

Web Part A

Type t = myButton.GetType(); object[] p = new object[1]; p[0] = EventArgs.Empty; MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance); m.Invoke(myButton, p);

Web Part B

public void btnHidden_Click(object sender, EventArgs e) { Label1.Text = "Hidden Button: " + DateTime.Now.ToString(); }

When I use reflection, I get the correct information on the HiddenButton. However, I cannot invoke the "OnClick" event. The btnHidden_Click does not execute. It works fine when I invoke from WebPart B to WebPart B, but not from a different webpart.

There doesn't appear to be too much information regarding this behavior. Any suggestions?

Thanks.

Rob

A: 

Since Web Parts should be a loosely coupled as possible, I'd use the Web Part connections infrastructure and the Subscriber pattern to handle this.

Communication between connected parts is done through an interface. This interface could include Subscribe and Unsubscribe methods. When the connection is made, the consumer part could subscribe any UpdatePanel controls it needs to be updated when the selected item changes in the provider. Then when the selected item does change, the provider part can walk its subscribers and call the Update method on the UpdatePanel to force an async postback.

I haven't tried this myself but the only drawback I can see is that you may have to queue the calls to Update because any triggering of an async postback cancels any other async postbacks currently in progress.

I hope this helps.

Rob Windsor
A: 

This is so incredably easy to solve using JavaScript. I don't want to say that Web Parts is bad (in as much as I've never worked with them), but I will say that if you are able to program the tiniest bit of JS into the web parts, the code is:

document.getElementById('ElID').onclick();

Good luck!

Mike