无法捕获ImageButton在回发事件中单击



无法捕获postback中的ImageButtonClick事件。

我正在使用以下代码进行按钮单击并尝试用于imageButton的"按钮"单击其工作,而不是用于图像按钮。

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;
    }

任何解决方案?

尝试使用以下方式替换按钮检查块:

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

获得了解决方案:

在上面提到的代码中添加了另一个检查, //处理imageButton postbacks

        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 potback事件。

谢谢

最新更新