如何从提供者 Web 部件到消费者 Web 部件调用 Ajax OnClick

发布于 2024-07-10 17:50:30 字数 760 浏览 7 评论 0原文

我试图通过在单独的 Web 部件上调用按钮 onclick 方法来管理 ajax 连接,以强制消费者进行部分回发。

Web 部件 A(提供者)调用 Web 部件 B(使用者)上的方法

Web 部件 A

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

Web 部分 B

public void btnHidden_​​Click(object sender, EventArgs e) { Label1.Text = "隐藏按钮:" + DateTime.Now.ToString(); 当

我使用反射时,我获得了有关 HiddenButton 的正确信息。 但是,我无法调用“OnClick”事件。 btnHidden_​​Click 不执行。 当我从 WebPart B 调用到 WebPart B 时,它工作正常,但不能从不同的 Webpart 调用。

似乎没有太多关于此行为的信息。 有什么建议么?

谢谢。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2024-07-17 17:50:30

由于 Web 部件应尽可能松散耦合,因此我将使用 Web 部件连接基础结构和订阅者模式来处理此问题。

连接部件之间的通信是通过接口完成的。 该接口可以包括订阅和取消订阅方法。 建立连接后,使用者部分可以订阅任何 UpdatePanel 控件,当提供者中的所选项目发生更改时,它需要更新。 然后,当所选项目发生更改时,提供程序部分可以遍历其订阅者并调用 UpdatePanel 上的 Update 方法来强制异步回发。

我自己没有尝试过,但我看到的唯一缺点是您可能必须对 Update 调用进行排队,因为任何异步回发的触发都会取消当前正在进行的任何其他异步回发。

我希望这有帮助。

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.

烟沫凡尘 2024-07-17 17:50:30

使用 JavaScript 可以非常容易地解决这个问题。 我不想说 Web 部件不好(因为我从未使用过它们),但我会说,如果您能够将最少量的 JS 编程到 Web 部件中,那么代码就是:

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

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文