您的位置:首页 > 其它

在Page_Load 中判断是哪个控件触发了postback

2006-09-01 13:19 567 查看
在Page_Load 中判断是哪个控件触发了postback(Detect in Page_Load which control caused a postback)

Private Function DidControlCausePostBack(ByVal uniqueID As String) As Boolean

        Dim bool As Boolean = False

        bool = (Not Request.Form(uniqueID) Is Nothing) OrElse (Not Request.Form("__EVENTTARGET") Is Nothing AndAlso Request.Form("__EVENTTARGET").Equals(uniqueID)) OrElse ((Not Request.Form(uniqueID & ".x") Is Nothing) AndAlso (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有效
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息