ASP.NET 动态命令按钮仅每隔一段时间触发一次

发布于 2024-09-14 05:52:08 字数 2279 浏览 1 评论 0原文

与此问题类似ASP.Net动态命令按钮事件未触发 但问题略有不同。

下面提供的是我的代码的(非常)精简版本。

    protected void Page_Load(object sender, EventArgs e)
    {
        RenderDataItems();
    }

    private void RenderDataItems()
    {
        pnlDataItems.Controls.Clear()
        DataTable dt = MyClass.GetAllData();

        foreach (DataRow dr in dt.Rows)
        {
            Button b = new Button();
            b.Command += new CommandEventHandler(SelectItem);
            b.CommandArgument = dr["ID"].ToString();
            b.ID = "btnData" + dr["ID"].ToString();

            if (hdnDataListID.Value == dr["ID"].ToString())
            {
                b.Text = "Selected Item";
            }
            else
            {
                b.Text = "Pick This Item";
            }
            pnlDataItems.Controls.Add(b);
        }
    }

    private void SelectItem(object sender, CommandEventArgs e)
    {
        hdnDataListID.Value = e.CommandArgument.ToString();
        RenderDataItems();
    }

    private void EditSelectItem(int id)
    {
        MyClass mc = new MyClass(id);
        hdnDataListID.Value = mc.ID.ToString();
        RenderDataItems();
    }

SelectItem 方法仅由 RenderDataItems 方法中呈现的按钮控件调用。 EditSelectItem 由动态创建的单独控件调用,但不需要 RenderDataItems 方法中的按钮进行所需的更改。

我已经运行了调试器并单步执行代码以查看发生了什么。加载页面时,从 PageLoad 调用 RenderDataItems 并使用具有“Pick This Text”的所有按钮填充面板(因为 HiddenField 控件的值 (<代码>hdnDataListID)尚未设置)。

第一次单击其中一个按钮时,PageLoad 中的 RenderDataItems 会触发,然后是按钮的初始填充,HiddenField 的值设置为 ID,并且第二个 RenderDataItems 调用发生在 SelectItem 方法中。按钮被清除并重新创建。正确的按钮具有“所选项目”文本。

第二次单击其中一个按钮时,PageLoad 中的 RenderDataItems 触发,然后是按钮的初始填充,但 SelectItem 方法从不火灾。

我第三次单击其中一个按钮时,会出现与第一次相同的功能。第四个模仿第二个。第五个模仿第一个。等等,等等。

当从不包含在面板中的控件中使用 EditSelectItem 方法时(它是一个 DataSource 绑定的 GridView 行,其中带有调用此方法的按钮),它的作用完全符合我的预期,并正确设置了选定/未选定按钮,每次都会调用 RenderDataItemsEditSelectItem 方法。

有什么想法吗?

PS 我已经删除了此页面上的所有 AJAX。

Similar to this question here ASP.Net Dynamic Command Button Event Not Firing but with a slightly different problem.

Provided below is a (very) condensed version of my code.

    protected void Page_Load(object sender, EventArgs e)
    {
        RenderDataItems();
    }

    private void RenderDataItems()
    {
        pnlDataItems.Controls.Clear()
        DataTable dt = MyClass.GetAllData();

        foreach (DataRow dr in dt.Rows)
        {
            Button b = new Button();
            b.Command += new CommandEventHandler(SelectItem);
            b.CommandArgument = dr["ID"].ToString();
            b.ID = "btnData" + dr["ID"].ToString();

            if (hdnDataListID.Value == dr["ID"].ToString())
            {
                b.Text = "Selected Item";
            }
            else
            {
                b.Text = "Pick This Item";
            }
            pnlDataItems.Controls.Add(b);
        }
    }

    private void SelectItem(object sender, CommandEventArgs e)
    {
        hdnDataListID.Value = e.CommandArgument.ToString();
        RenderDataItems();
    }

    private void EditSelectItem(int id)
    {
        MyClass mc = new MyClass(id);
        hdnDataListID.Value = mc.ID.ToString();
        RenderDataItems();
    }

The method SelectItem is only called by the button controls rendered within the RenderDataItems method. The EditSelectItem is called by a separate control that is created dynamically, but does not require the altering that the buttons in the RenderDataItems method requires.

I've run the debugger and stepped through the code to see what happens. When the page is loaded, the RenderDataItems is called from the PageLoad and populates the panel with all of the buttons having "Pick This Text" (because the HiddenField control's value (hdnDataListID) has not been set).

The first time I click one of the buttons, the RenderDataItems from PageLoad fires, followed by the initial population of the buttons, the HiddenField's value is set to the ID, and the second RenderDataItems call happens from within the SelectItem method. The buttons are cleared, and recreated. The correct button has the "Selected Item" text.

The second time I click one of the buttons, the RenderDataItems from PageLoad fires, followed by the initial population of the buttons, but the SelectItem method never fires.

The third time I click one of the buttons, the same functionality happens as the 1st time. The 4th mimics the 2nd. The 5th mimics the 1st. And so on, and so on.

When using the EditSelectItem method from the controls not contained within the panel (it's a DataSource bound GridView row with buttons that calls this method), it does exactly as I'd expect and properly sets the selected / unselected buttons, with calls to both the RenderDataItems and the EditSelectItem method every time.

Any ideas?

P.S. I've already removed all of my AJAX on this page.

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

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

发布评论

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

评论(1

梦境 2024-09-21 05:52:08

您应该为您的 Button b 提供一个 ID。

You should give your Button b an ID.

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