【问题标题】:Get reference of image button click event in gridview in http module在http模块的gridview中获取图像按钮单击事件的引用
【发布时间】:2012-02-14 22:12:19
【问题描述】:

我们如何将ImageButton 中的Click 事件在GridView 中传递给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

【问题讨论】:

    标签: asp.net httpmodule


    【解决方案1】:

    如果您尝试将事件附加到 gridview 中的按钮,我可能会建议在 prerender 事件的基本页面中解析页面上的所有 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() 是一个扩展方法,定义如下(从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;
        }
    }
    

    【讨论】:

    • 我正在尝试这样做,但它不会来。这是我们这样做的方式吗?我正在做这个预先请求手执行 HttpContext httpContext = (HttpContext)httpApplication.Context; Page page = (HttpContext.Current.Handler as Page); if (page != null) { foreach (Control childControl in page.Controls) { if (childControl is GridView) { } } }
    • 你试过把它放在你的基本页面代码中吗?此外,如果您的网格视图包含在其他元素中,因此不直接位于页面下方,您的代码将找不到它们
    • 是的,我将它保存在 http 模块页面本身中。
    • 尝试将其移动到物理页面而不是模块中,您可能会发现如果代码未在预渲染或类似类型的事件中执行,则网格尚未绑定数据且没有按钮会出现在其中,因此什么也找不到
    • 还没有。我猜这对我不起作用。我在类库中有一个 http 模块,我希望一切都发生在那里。我不想触及任何部分.net application.all 我所做的就是在 web.config 中插入引用,它应该可以工作
    【解决方案2】:

    ImageButton 的名称中有一个额外的准属性,用于标识鼠标坐标(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
        }
    }
    

    您还可以认为this answer 很有用。它包含一个更通用的方法来确定哪个控件导致了回发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2016-02-13
      • 2017-12-09
      相关资源
      最近更新 更多