Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining which control caused PostBack

Tags:

c#

asp.net

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?

like image 560
Majid Avatar asked Aug 20 '26 14:08

Majid


2 Answers

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
}
like image 181
Samiey Mehdi Avatar answered Aug 22 '26 05:08

Samiey Mehdi


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

like image 27
Mister Epic Avatar answered Aug 22 '26 04:08

Mister Epic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!