获取该发射了AJAX的UpdatePanel内的回发ASP.NET控件 [英] Get ASP.NET control which fired a postback within a AJAX UpdatePanel

查看:81
本文介绍了获取该发射了AJAX的UpdatePanel内的回发ASP.NET控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此相关的问题:<一href=\"http://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event\">On回发,我怎么能检查哪些控制Page_Init事件回发

如果控件被包装在一个ASP.NET AJAX的UpdatePanel,变量控制是空的,因为它具有AJAX回发后不同的ID。
有没有一种解决方案,使该发射的ASP.NET AJAX的UpdatePanel?

内的回传控制

 公共静态字符串GetPostBackControlName(页页){
        控制控制= NULL;        / **
         *首先,我们将检查__EVENTTARGET,因为如果回发是由
         *由其中所用的_doPostBack功能控制,这将是Request.Form集合中可用。
         * /
        字符串ctrlname = page.Request.Params [__ EVENTTARGET];        如果(!String.IsNullOrEmpty(ctrlname)){
            控制= page.FindControl(ctrlname);
        }其他{
            / **
             *如果__EVENTTARGER为空,控制按钮式和
             *需要遍历窗体集合找到它。
             * /
            控制C = NULL;
            字符串ctrlStr = NULL;            的foreach(在page.Request.Form串CTL){
                如果(ctl.EndsWith(.X)|| ctl.EndsWith(.Y)){
                    / **
                     * ImageButtons有自己的身份证一个额外的准物权标识
                     *鼠标坐标(X和Y)。
                     * /
                    ctrlStr = ctl.Substring(0,ctl.Length - 2);
                    C = page.FindControl(ctrlStr);
                }其他{
                    C = page.FindControl(CTL);
                }                如果(c是按钮|| c为的ImageButton){
                    控制= C;
                    打破;
                }
            }
        }        如果(控制!= NULL){
            返回control.ID;
        }        返回的String.Empty;
    }


解决方案

请尝试以下方法:

 公共字符串GetAsyncPostBackControlID()
{
    字符串smUniqueId = ScriptManager.GetCurrent(页).UniqueID;
    字符串smFieldValue =的Request.Form [smUniqueId]    如果(String.IsNullOrEmpty(smFieldValue)及&放大器; smFieldValue.Contains('|')!)
    {
        返回smFieldValue.Split('|')[1];
    }    返回的String.Empty;
}

以上方法使用的ScriptManager的隐藏字段页面上。它的值可以在服务器上通过搜索使用ScriptManager的UniqueID形式键来访问。在隐藏字段中的值的格式为 [UpdatePanel中的UniqueID] | [回传控制ID] 。知道了这些信息,我们可以检索启动异步回发的控件的ID。和它的作品的提交按钮了。

Related to this question: On postback, how can I check which control cause postback in Page_Init event

If the control is wrapped in an ASP.NET AJAX UpdatePanel, the variable "control" is empty because it has a different ID after the AJAX PostBack. Is there a solution to get the control which fired a postback within an ASP.NET Ajax UpdatePanel?

public static string GetPostBackControlName( Page page ) {
        Control control = null;

        /**
         * First we will check the "__EVENTTARGET" because if the postback is made
         * by controls which used the _doPostBack function, it will be available in the Request.Form collection.
         */
        string ctrlname = page.Request.Params["__EVENTTARGET"];

        if ( !String.IsNullOrEmpty( ctrlname ) ) {
            control = page.FindControl( ctrlname );
        } else {
            /**
             * If __EVENTTARGER is null, the control is a button-type and
             * need to iterate over the form collection to find it.
             */
            Control c = null;
            string ctrlStr = null;

            foreach ( string ctl in page.Request.Form ) {
                if ( ctl.EndsWith( ".x" ) || ctl.EndsWith( ".y" ) ) {
                    /**
                     * ImageButtons have an additional "quasi-property" in their ID which identifies
                     * the mouse-coordinates (X and Y).
                     */
                    ctrlStr = ctl.Substring( 0, ctl.Length - 2 );
                    c = page.FindControl( ctrlStr );
                } else {
                    c = page.FindControl( ctl );
                }

                if ( c is Button || c is ImageButton ) {
                    control = c;
                    break;
                }
            }
        }

        if ( control != null ) {
            return control.ID;
        }

        return string.Empty;
    }

解决方案

Try the following method:

public string GetAsyncPostBackControlID()
{
    string smUniqueId = ScriptManager.GetCurrent(Page).UniqueID;
    string smFieldValue = Request.Form[smUniqueId];

    if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains('|'))
    {
        return smFieldValue.Split('|')[1];
    }

    return String.Empty;
}

The above method uses the hidden field of the ScriptManager on page. Its value can be accessed on the server by searching for a form key with the UniqueID of the ScriptManager. The value in the hidden field is in the format [UpdatePanel UniqueID]|[Postback Control ID]. Knowing this information we can retrieve the ID of the control that initiated the asynchronous postback. And it works for submit buttons too.

这篇关于获取该发射了AJAX的UpdatePanel内的回发ASP.NET控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆