【问题标题】:Get ASP.NET control which fired a postback within a AJAX UpdatePanel获取在 AJAX UpdatePanel 中触发回发的 ASP.NET 控件
【发布时间】:2010-09-29 12:31:53
【问题描述】:

与本题相关:On postback, how can I check which control cause postback in Page_Init event

如果控件包装在 ASP.NET AJAX UpdatePanel 中,则变量“control”为空,因为它在 AJAX PostBack 之后具有不同的 ID。 是否有解决方案来获取在 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;
    }

【问题讨论】:

    标签: asp.net asp.net-ajax


    【解决方案1】:

    试试下面的方法:

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

    上述方法使用页面上ScriptManager的隐藏字段。它的值可以通过在服务器上搜索具有 ScriptManager 的 UniqueID 的表单键来访问。隐藏字段中的值格式为[UpdatePanel UniqueID]|[Postback Control ID]。知道了这些信息,我们可以检索启动异步回发的控件的 ID。它也适用于提交按钮。

    【讨论】:

    • 不错!只是对代码的一个非常小的更新; .Contains('|') 应该是 .Contains("|") :o)
    【解决方案2】:

    这是@bugventure 在 VB.NET 中的代码...修改为添加页面引用作为参数,以便我可以在公共库中使用它。

    Public Function GetAsyncPostBackControlID(ByRef page As Page) As String
            Dim smUniqueId As String = ScriptManager.GetCurrent(page).UniqueID
            Dim smFieldValue As String = page.Request.Form(smUniqueId)
    
            If Not String.IsNullOrEmpty(smFieldValue) AndAlso smFieldValue.Contains("|") Then
                Return smFieldValue.Split("|")(1)
            End If
    
            Return String.Empty
    End Function
    

    用法:

    ' Call from a control or Page
    Dim postbackControlID As String = GetAsyncPostBackControlID(Page)
    
    ' Call from a page
    Dim postbackControlID As String = GetAsyncPostBackControlID(Me)
    

    【讨论】:

    • 这个方法传什么?在 bugventure 的帖子中,他的方法不带参数。
    • 我传入了对 Page 对象的引用。这样该函数就可以在通用库中使用。在 Bugventure 的版本中,使用 Control.Page 属性检索页面引用,该属性存储对包含控件的 Page 的引用。当在 Page 中调用 this 时,它引用自身,与使用“Me”相同。我将添加一个使用示例。
    • 哦,谢谢。我是 VB 开发的新手,所以我不确定“我”是否真的是正确的。
    【解决方案3】:

    要获取导致异步回发的控件,可以使用以下代替解析

    var scriptManager = ScriptManager.GetCurrent(Page);
    var asyncPostBackSourceControl = Page.FindControl(scriptManager.AsyncPostBackSourceElementID);
    

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 2012-10-01
      • 2010-11-03
      • 1970-01-01
      • 2014-07-30
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      相关资源
      最近更新 更多