__EVENTTARGET is empty on postback of button click

前端 未结 5 1981
灰色年华
灰色年华 2020-12-09 17:35

I have a button on aspx page



        
相关标签:
5条回答
  • 2020-12-09 18:10

    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.

    0 讨论(0)
  • 2020-12-09 18:14
    /// <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

    0 讨论(0)
  • 2020-12-09 18:22

    Just add UseSubmitBehavior="False" to your button.

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

    0 讨论(0)
  • 2020-12-09 18:24

    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;
    
            }
    
    0 讨论(0)
  • 2020-12-09 18:27

    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.

    0 讨论(0)
提交回复
热议问题