3

I have this aspx:

<asp:ImageButton ID="check" runat="server" ImageUrl="../img/process.png"  OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>

now in Page_Load I want to determine that is PostBack caused by check or no so I followed this question's method with this code:

if(FindControl(Page.Request.Params.Get("__EVENTTARGET"))!=check)//if not caused by "check"
    //do something

but Page.Request.Params.Get("__EVENTTARGET") is empty .(I'm using my ImageButton in UpdatePanel)

How can I reach to my goal?

Community
  • 1
  • 1
Majid
  • 13,853
  • 15
  • 77
  • 113
  • You might try and see it in IE Developer tools. It will tell you which request sent had a 404 or whatever error code. Which caused the issue. It will also help you to understand which request provides you a response. And also the time elapsed! In IE press F12. – Afzaal Ahmad Zeeshan Aug 18 '13 at 03:36
  • excuse me ; now I'm busy; I'll check answers in next hours!!! – Majid Aug 18 '13 at 03:40
  • this answer is shorter :) http://stackoverflow.com/questions/7269271/which-control-caused-the-postback – R.Akhlaghi Apr 08 '15 at 10:31

4 Answers4

7
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        return;
    Control control = null;
    string controlName = Page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = Page.FindControl(controlName);
    }
    else
    {
        string controlId;
        Control foundControl;
        foreach (string ctl in Page.Request.Form)
        {
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = Page.FindControl(controlId);
            }
            else
            {
                foundControl = Page.FindControl(ctl);
            }
            if (!(foundControl is Button || foundControl is ImageButton)) continue;
            control = foundControl;
            break;
        }
    }
    Label1.Text = control.ID; // label1 must be in UpdatePanel
}
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63
1

Try this:

Control ctrl = null;

string target = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(target))
    ctrl = page.FindControl(target);

if(ctrl == check){
     //check is the control that caused postback
}

** UPDATE **

Ok, turns out ImageButtons function a little differently. Use this for your markup:

<asp:ImageButton onClientClick="setTarget(this.id)" ID="check" runat="server" ImageUrl="../img/process.png"  OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>

<asp:HiddenField ID="targetId" runat="server" />

Now create a javascript function that will populate our hidden field with the ID of the field that initiates the PostBack:

function SetSource(id)
{
    var targetId=
    document.getElementById("<%=targetId.ClientID%>");
    targetId.value = id;
}

And finally we check it in our PostBack:

Control ctrl = null;

if (Request.Form[targetId.UniqueID] != null &&
    Request.Form[targetId.UniqueID] != string.Empty)
{
    ctrl = Page.FindControl(Request.Form[targetId.UniqueID]);
}

if(ctrl == check){
 //check is the control that caused postback
}

ref: http://www.aspsnippets.com/Articles/How-to-find-the-control-that-caused-PostBack-in-ASP.Net.aspx

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
1

Since this is an update panel, then try getting the value via the ScriptManager, like this:

protected void Page_Load(object sender, EventArgs e)
{
    var updatePanelControlIdThatCausedPostBack = String.Empty;
    var scriptManager = ScriptManager.GetCurrent(Page);

    if (scriptManager != null)
    {
        var smUniqueId = scriptManager.UniqueID;
        var smFieldValue = Request.Form[smUniqueId];

        if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|"))
        {
            updatePanelControlIdThatCausedPostBack = smFieldValue.Split('|')[1];
        }
    }

    if (!String.IsNullOrEmpty(updatePanelControlIdThatCausedPostBack))
    {
        // Do something with control ID value that caused UpdatePanel postback here
    }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

If a control causes PostBack its ID will be non null in the request collection, Form.Request[controlID].

In case of an Image, there are two items in the request collection, one for the x coordinate and one for the y coordinate for where exactly in the image the mouse was clicked.

So do:

if(Form.Request["Img1.x"] != null)
Majid
  • 13,853
  • 15
  • 77
  • 113
Andyz Smith
  • 698
  • 5
  • 20