按钮点击回发后__EVENTTARGET为空

时间:2013-12-30 10:51:05

标签: c# asp.net

我在aspx页面上有一个按钮

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

我正在尝试在代码中获取事件目标和事件源,以便根据它进行一些验证。我尝试使用下面的代码。

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

但以上所有都给了我空值。如何获得每次都引起回发的控件。我上面做错了什么?

仅供参考:我已经尝试了这个LINK中提到的解决方案。但它唯一的返回按钮文本给我。我想要buttonID。

5 个答案:

答案 0 :(得分:13)

Asp按钮呈现input类型submit,此方法无法填充_EVENTTARGET 使用"__doPostBack"方法导致回发的控件会将值添加到_EVENTTARGET 因此_EVENTTARGET中缺少您的按钮ID,您可以遍历页面中的所有控件以检查哪个控件导致回发。

尝试此操作以捕获您的控件 - 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;

        }

答案 1 :(得分:8)

到目前为止,我已将usesubmitbehavior设置为false。 Damith在评论中给出了一个很好的解决方案。截至目前,它对我来说没有任何问题。要了解财产Read this LINK

希望这会对某人有所帮助。

答案 2 :(得分:2)

只需在按钮上添加UseSubmitBehavior =“False”。

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

答案 3 :(得分:1)

/// <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

答案 4 :(得分:0)

我的解决方案。我在客户端添加这样的OnClick事件

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

此处s是在DevExpress中显示我的按钮的对象,其中属性name包含作为el的Id的一部分。所以,在服务器端,我可以获得发送回发的按钮的Id。价格是服务器OnClick enevnt没有被解雇。