0

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.

Oleks
  • 31,955
  • 11
  • 77
  • 132
Yoda
  • 319
  • 2
  • 5
  • 20

2 Answers2

0

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;
    }
}
Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
  • I am trying to do this way but it's not coming.is this the way we do .i am doing this in prerequest hand execute 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) { } } } – Yoda Feb 14 '12 at 23:01
  • have you tried putting it in your base page code? Also if your gridviews are contained in other elements and hence not directly under the page your code would not find them – Daniel Powell Feb 14 '12 at 23:05
  • Yup i kept that in the http module page itself. – Yoda Feb 14 '12 at 23:06
  • try moving it to a physical page and not in the module, you might find that if the code doesn't execute in a prerender or similar type of event that the grid has not been databound and no buttons would appear in it and hence there would be nothing to find – Daniel Powell Feb 14 '12 at 23:13
  • no not yet.this doesn't work me i guess.i have a http module in a class library and i want everything to happen form there itself.i don't want to touch any part of the .net application.all i do is plug the reference in web.config and it should work – Yoda Feb 15 '12 at 04:03
0

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.

Community
  • 1
  • 1
Oleks
  • 31,955
  • 11
  • 77
  • 132