首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取在AJAX UpdatePanel中触发回发的ASP.NET控件

获取在AJAX UpdatePanel中触发回发的ASP.NET控件
EN

Stack Overflow用户
提问于 2010-09-29 20:31:53
回答 3查看 7.1K关注 0票数 4

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

如果控件包装在ASP.NET AJAX控制中,则变量“UpdatePanel”为空,因为它在AJAX PostBack之后具有不同的ID。有没有解决方案来获取在ASP.NET Ajax UpdatePanel中触发回发的控件?

代码语言:javascript
复制
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;
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-10-01 03:20:17

尝试以下方法:

代码语言:javascript
复制
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。它也适用于提交按钮。

票数 9
EN

Stack Overflow用户

发布于 2011-03-30 10:32:31

下面是@bugventure在VB.NET中的代码……修改后添加了一个页面引用作为参数,这样我就可以在一个公共库中使用它。

代码语言:javascript
复制
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

用法:

代码语言:javascript
复制
' Call from a control or Page
Dim postbackControlID As String = GetAsyncPostBackControlID(Page)

' Call from a page
Dim postbackControlID As String = GetAsyncPostBackControlID(Me)
票数 1
EN

Stack Overflow用户

发布于 2016-08-31 07:33:04

若要获取导致异步回发的控件,可以使用以下代码而不是分析

代码语言:javascript
复制
var scriptManager = ScriptManager.GetCurrent(Page);
var asyncPostBackSourceControl = Page.FindControl(scriptManager.AsyncPostBackSourceElementID);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3821569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档