如何在javascript中获取控件的id导致回发

如何在javascript中获取控件的id导致回发,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,如何在asp.net中获取导致PostBack的控件id。我用 document.getElementById("__EVENTTARGET") 但是它只提供了objectHtmlInputElement而不是一个完整的ID。我怎样才能只在javascript中而不是在代码隐藏中获得它呢。我想在回发后关注这个元素。请帮助..您可以使用本文中提到的此函数获取回发控件id,将此id存储在hiddenfield中,并使用JS从hiddenfield获取该id protected void Page

如何在asp.net中获取导致
PostBack
的控件id。我用

document.getElementById("__EVENTTARGET")

但是它只提供了
objectHtmlInputElement
而不是一个完整的ID。我怎样才能只在javascript中而不是在代码隐藏中获得它呢。我想在回发后关注这个元素。请帮助..

您可以使用本文中提到的此函数获取回发控件id,将此id存储在hiddenfield中,并使用JS从hiddenfield获取该id

 protected void Page_Load(object sender, EventArgs e)
    {    
        if (Page.IsPostBack)
           HiddenField1.Value = getPostBackControlName();
    } 

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;
}

使用
document.getElementById(“\uu EVENTTARGET”).value

您可以从codebehind将控件id存储在hiddenfield中,并读取在回发后使用javascript它不是特定的控件,它可以是任何控件。是的,我说的是所有控件。在下面检查我的答案谢谢你的回答,但我没有得到javascript中隐藏的字段值。我使用了'var str=$('HiddenField1').val();'我在函数
getPostBackControlName
中使用了一个全局变量
controlid
而不是隐藏字段和
control.ClientID
,并通过
ScriptManager.RegisterStartupScript将其设置为全局变量(Page,Page.GetType(),“updateJavaScriptId”,“controlid=”+getPostBackControlName()+“;”,true)。它对我有用。