获取http模块中gridview中图片按钮点击事件的引用

发布于 2025-01-05 19:50:17 字数 396 浏览 0 评论 0原文

我们如何将GridView内的ImageButtonClick事件传递给httpmodule 对于链接按钮,我这样做:

if (request.Form.GetValues("__EVENTTARGET") != null)
{

    //If it's a link button execute we can directley check for the params 
    if (request.Params.Get("__EVENTTARGET").Contains("xyz"))
    {
        //some Code
    }

这不适用于ImageButton

How do we pass the Click event of ImageButton inside a GridView to httpmodule
for linkbutton's i am doing this way:

if (request.Form.GetValues("__EVENTTARGET") != null)
{

    //If it's a link button execute we can directley check for the params 
    if (request.Params.Get("__EVENTTARGET").Contains("xyz"))
    {
        //some Code
    }

This is not working for ImageButton.

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

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

发布评论

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

评论(2

看透却不说透 2025-01-12 19:50:17

如果您尝试将事件附加到 gridview 中的按钮,我可能会建议您在预渲染事件的基页中解析页面上的所有 gridview(使用递归 findcontrol 算法)并查找任何图像按钮(如果找到)然后您应该能够向其附加一个事件。

编辑:
我在下面使用类似的东西:

 public abstract class AmendmentPopUpWindow : BaseMasterPlanPage
    {
        // override this method if the correct save controls arent being hidden in the popups
        public virtual IEnumerable<WebControl> SaveControls
        {
            get { return Controls.All().OfType<WebControl>().Where(c => c.ID.ToLower().Contains("save")); }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (WebConfiguration.Global_EnableAmendments && SystemVersion.HasValue)
            {
                foreach (var control in Controls.All())
                {
                    if (control is RadioButton || control is TextBox || control is DropDownList || control is RadComboBox || control is CheckBox || control is CheckBoxList ||
                        control is RadEditor || control is RadTextBox || control is RadNumericTextBox)
                    {
                        var webControl = control as WebControl;
                        webControl.Enabled = false;
                        webControl.ForeColor = Color.Gray;
                    }
                }

                foreach (var saveControl in SaveControls)
                    saveControl.Visible = false;
            }

            base.OnPreRender(e);
        }

编辑: .All() 是一个定义如下的扩展方法(从 此处

public static IEnumerable<Control> All(this ControlCollection controls)
{
    foreach (Control control in controls)
    {
        foreach (Control grandChild in control.Controls.All())
            yield return grandChild;

        yield return control;
    }
}

If you're trying to attach an event to a button within a gridview might I suggest in your base page on the prerender event parse through all gridviews on the page (use a recursive findcontrol algorithm) and look for any imagebuttons, if you find one you should then be able to attach an event to it.

EDIT:
I use something similar in the following:

 public abstract class AmendmentPopUpWindow : BaseMasterPlanPage
    {
        // override this method if the correct save controls arent being hidden in the popups
        public virtual IEnumerable<WebControl> SaveControls
        {
            get { return Controls.All().OfType<WebControl>().Where(c => c.ID.ToLower().Contains("save")); }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (WebConfiguration.Global_EnableAmendments && SystemVersion.HasValue)
            {
                foreach (var control in Controls.All())
                {
                    if (control is RadioButton || control is TextBox || control is DropDownList || control is RadComboBox || control is CheckBox || control is CheckBoxList ||
                        control is RadEditor || control is RadTextBox || control is RadNumericTextBox)
                    {
                        var webControl = control as WebControl;
                        webControl.Enabled = false;
                        webControl.ForeColor = Color.Gray;
                    }
                }

                foreach (var saveControl in SaveControls)
                    saveControl.Visible = false;
            }

            base.OnPreRender(e);
        }

EDIT: The .All() is an extension method defined as follows (stolen from here)

public static IEnumerable<Control> All(this ControlCollection controls)
{
    foreach (Control control in controls)
    {
        foreach (Control grandChild in control.Controls.All())
            yield return grandChild;

        yield return control;
    }
}
笨死的猪 2025-01-12 19:50:17

ImageButtons 的名称中有一个附加的准属性,用于标识鼠标坐标(X 和 Y)。

因此,要查找 ImageButton 的名称,您应该迭代发布的参数并找到以 .x.y 结尾的参数:

foreach (string item in request.Form)
{
    if (item.EndsWith(".x") || item.EndsWith(".y"))
    {
        var controlName = item.Substring(0, item.Length - 2);
        // some code here
    }
}

您还可以查找 这个答案很有用。它包含一个更通用的方法来确定哪个控件导致了回发。

ImageButtons have an additional quasi-property in their names which identifies the mouse-coordinates (X and Y).

So to find the ImageButton's name your should iterate through posted parameters and found those which end with .x or .y:

foreach (string item in request.Form)
{
    if (item.EndsWith(".x") || item.EndsWith(".y"))
    {
        var controlName = item.Substring(0, item.Length - 2);
        // some code here
    }
}

You could also cound this answer useful. It contains a more generic method to determine which control caused a postback.

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