【问题标题】:unable to capture ImageButton click in postback event无法在回发事件中捕获 ImageButton 单击
【发布时间】:2012-11-06 12:02:08
【问题描述】:

无法在回发中捕获 Imagebuttonclick 事件。

我正在使用以下代码进行按钮单击并尝试使用 Imagebutton,但是“Button”单击其工作而不是 Image 按钮。

public Control GetPostBackControl(Page page)
    {
        Control control = null;

        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if ((ctrlname != null) & ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }

有什么办法吗?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    尝试用这个替换你的按钮检查块:

    if (c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
    

    【讨论】:

    • 这将不起作用,因为 Button 和 Imagebutton 点击​​事件过程不同。我已经得到了解决方案并发布了相同的内容。
    【解决方案2】:

    找到解决办法:

    在上述代码中添加了一项检查, // 处理 ImageButton 回发

            if (control == null)
            {
                for (int i = 0; i < page.Request.Form.Count; i++)
                {
    
                    if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y")))
                    {
                        control = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2));                     
                    }
                }
            }
    

    现在我可以捕获 ImageButton 回发事件了。

    谢谢

    【讨论】:

    • 你在哪里添加了这张支票?你能发布你的完整代码吗?
    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2013-06-28
    相关资源
    最近更新 更多