2

PostBackasp.netで発生するコントロールのIDを取得するにはどうすればよいですか。私は使用します

document.getElementById("__EVENTTARGET")

しかし、それはobjectHtmlInputElement完全なIDではありません。コードビハインドではなく、JavaScriptのみでこれを取得するにはどうすればよいですか。ポストバック後にその要素に焦点を当てるためにこれが必要です。助けてください..

4

2 に答える 2

4

このブログで言及されているこの関数を使用して、ポストバック コントロール 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;
}
于 2012-12-27T12:42:29.020 に答える
1

使用するdocument.getElementById('__EVENTTARGET').value

于 2014-01-28T19:02:16.823 に答える