现在的位置: 首页 > 综合 > 正文

在Page_Load 中判断是哪个控件触发了postback(Detect in Page_Load which control caused a postback)

2012年08月01日 ⁄ 综合 ⁄ 共 1486字 ⁄ 字号 评论关闭
Private Function DidControlCausePostBack(ByVal uniqueID As StringAs Boolean

        Dim bool As Boolean = False

        bool = (Not Request.Form(uniqueID) Is NothingOrElse (Not Request.Form("__EVENTTARGET"Is Nothing AndAlso Request.Form("__EVENTTARGET").Equals(uniqueID)) OrElse ((Not Request.Form(uniqueID & ".x"Is NothingAndAlso (Not Request.Form(uniqueID & ".y"Is Nothing))

        Return bool

    End Function

private bool DidControlCausePostBack(string uniqueID)
{

 return (!(Request.Form[uniqueID] == null)) || (!(Request.Form["__EVENTTARGET"== null&& Request.Form["__EVENTTARGET"].Equals(uniqueID)) || ((!(Request.Form[uniqueID + ".x"== null)) && (!(Request.Form[uniqueID + ".y"== null)));
}

Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
Protected WithEvents ImageButton1 As System.Web.UI.WebControls.ImageButton
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If DidControlCausePostBack(Button1.UniqueID) Then
            Response.Write(
"Button1")
        
End If

        If DidControlCausePostBack(LinkButton1.UniqueID) Then
            Response.Write(
"LinkButton1")
        
End If

        If DidControlCausePostBack(ImageButton1.UniqueID) Then
            Response.Write(
"ImageButton1")
        
End If

    End Sub

该方法仅对Buttons, LinkButtons and ImageButtons有效
From: http://aspadvice.com/blogs/joteke/archive/2004/08/05/2288.aspx

抱歉!评论已关闭.