18

I have a button on aspx page

<asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" />

I am trying to get the event target and event source in code behind to do some validation based on it. I tried with below code.

string ctrlname = page.Request.Params.Get("__EVENTTARGET");
string ctrlname = Request.Form["__EVENTTARGET"];
string ctrlname = Request.Params["__EVENTTARGET"];

But all the above are giving me empty values. How to get the control which caused postback everytime. Am i doing anyting wrong above?

FYI : I already tried the solution mentioned in this LINK. But its only returning button text for me. I want the buttonID.

Community
  • 1
  • 1
Naveen
  • 1,496
  • 1
  • 15
  • 24
  • 10
    try with ` – Damith Dec 30 '13 at 10:56
  • @Damith Thanks for the comment. Is there any problem setting this property to "false"? I see some answers below by Vignesh Kumar and Suraj Singh, That is also a working solution. But your comment is easier compare to them. I went through "http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.110).aspx". But i din't get any answer to "Is it causes any behavioral change of button?" – Naveen Dec 30 '13 at 11:10
  • @Naveen when we do `usesubmitbehavior="false"` the `type` of `input` changes from `submit` to `button` – vikas May 11 '14 at 17:03
  • This change has an impact : the form is submittable with javascript disabled with a submit type. But with a button type, the form will be no more submittable without javascript. (Maybe it is not an issue in your case, of course.) – Frédéric Sep 03 '14 at 08:35

5 Answers5

17

Asp button renders as input with type submit this method will not fill_EVENTTARGET controls using "__doPostBack" method to cause postback will add values to _EVENTTARGET so your button id is missing from _EVENTTARGET you can iterate through all the controls in page to check which control caused postback.

Try this to capture Your control -Here

private string getPostBackControlName()
        {
            Control control = null;
            //first we will check the "__EVENTTARGET" because if post back made by       the controls
            //which used "_doPostBack" function also available in Request.Form collection.
            string ctrlname = Page.Request.Params["__EVENTTARGET"];
            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = Page.FindControl(ctrlname);
            }
            // if __EVENTTARGET is null, the control is a button type and we need to
            // iterate over the form collection to find it
            else
            {
                string ctrlStr = String.Empty;
                Control c = null;
                foreach (string ctl in Page.Request.Form)
                {
                    //handle ImageButton they having an additional "quasi-property" in their Id which identifies
                    //mouse x and y coordinates
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        ctrlStr = ctl.Substring(0, ctl.Length - 2);
                        c = Page.FindControl(ctrlStr);
                    }
                    else
                    {
                        c = Page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.Button ||
                             c is System.Web.UI.WebControls.ImageButton)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control.ID;

        }
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
  • Thanks Suraj for the answer. I have some doubts which i added as comment under my question. Do you have any thoughts on that? – Naveen Dec 30 '13 at 11:16
  • @Naveen `UseSubmitBehavior` property specifies if the Button control uses the browser's built-in submit function or the ASP.NET postback mechanism. So i do not think using `Damien` solution is going to cause any issues.See when button renders as ` – Suraj Singh Dec 30 '13 at 11:21
  • 1
    @ Suraj Thank you. Even i thought so. But you also gave a good solution, up-vote for that. – Naveen Dec 30 '13 at 11:24
  • I think the answer [linked here](http://stackoverflow.com/a/3509755/2912011) is a little bit more comprehensive. Still a good answer though. – David Rogers Jan 04 '17 at 18:53
  • @David Rogers Thanks for the appreciation I will try to make this answer more efficient soon. – Suraj Singh Jan 05 '17 at 06:28
9

I have set usesubmitbehavior to false as of now. A good solution given by Damith above in the comment. As of now its working fine for me without any problem. To know about the property Read this LINK

Hope this will help someone.

Naveen
  • 1,496
  • 1
  • 15
  • 24
4

Just add UseSubmitBehavior="False" to your button.

<asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" UseSubmitBehavior="False" />

2
/// <summary>
/// Gets the ID of the post back control.
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    string controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            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;
        }
    }

    return control == null ? String.Empty : control.ID;
}

Source

Community
  • 1
  • 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0

My solution.I add OnClick event like this on my client side

function btnClickSuper(s, e) { __doPostBack(s.name, ''); }

here s is object presented my button in DevExpress, where property name contains, as part of, an Id of elment. so ,on server side, I can get Id of button sent postback. Price is server OnClick enevnt isn't fired.