如何使用ajax UpdatePanel注册GridView标题中按钮的点击事件

发布于 2024-08-02 02:41:02 字数 267 浏览 1 评论 0原文

我有一个包含 GridView 的 UpdatePanel,其中包含 TemplateField 的 HeaderTemplate 中的一个按钮。 我想将此按钮的单击事件添加到 UpdatePanel 的触发器集合中,但这似乎不起作用,因为我收到一条错误消息,指出无法找到具有指定 ID 的控件。

我想在页面加载时以编程方式添加到 UpdatePanel 的触发器集合,但这似乎不可能。

这个问题有解决方法吗? 理想情况下,我希望将按钮保留在 GridView 的标题中。

I have an UpdatePanel containing a GridView which contains a button in the HeaderTemplate of a TemplateField. I want to add this button's click event to the UpdatePanel's trigger collection but this doesn't seem to work as I get an error message saying that a control with the specified ID could not be found.

I thought of programmatically adding to the UpdatePanel's trigger collection on page load but this doesn't seem to be possible.

Is there a work-around to this problem? I'd ideally like to keep my button within the header of the GridView.

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

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

发布评论

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

评论(2

苏璃陌 2024-08-09 02:41:03

我通过访问页面上的 ScriptManager 而不是 UpdatePanel 本身解决了这个问题。 我在 Page_Load 方法中执行了此操作。 我的代码如下:

if (!Page.IsPostBack)
{
    Button button = GridView1.HeaderRow.FindControl("myHeaderButton") as Button;
    if (button != null)
        scriptManager.RegisterAsyncPostBackControl(button);
}

I've solved this by accessing the ScriptManager on the page instead of the UpdatePanel itself. I did this inside the Page_Load method. My code is as follows:

if (!Page.IsPostBack)
{
    Button button = GridView1.HeaderRow.FindControl("myHeaderButton") as Button;
    if (button != null)
        scriptManager.RegisterAsyncPostBackControl(button);
}
画尸师 2024-08-09 02:41:03

对不起,VB代码。 我没有能力测试这个(而且有可能它还很遥远,并且可能有更好的方法来做到这一点),但它应该为您指明正确的方向:

Dim g As GridView = UpdatePanel1.FindControl(nameofgridview)
Dim Button As Button = g.HeaderRow.FindControl(nameofbutton)
Dim u As UpdatePanel = Page.FindControl(panelID)
Dim trigger As UpdatePanelControlTrigger
trigger.ControlID = Button
u.Triggers.Add(trigger)

Sorry for the VB code. I don't have the ability to test this (and there is a chance that it is way off, and there is probably a better way to do it), but it should point you in the right direction:

Dim g As GridView = UpdatePanel1.FindControl(nameofgridview)
Dim Button As Button = g.HeaderRow.FindControl(nameofbutton)
Dim u As UpdatePanel = Page.FindControl(panelID)
Dim trigger As UpdatePanelControlTrigger
trigger.ControlID = Button
u.Triggers.Add(trigger)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文