I had one problem that need to be addressed is when Button1 clicked to call Button2 postback.
You can achieve it by writing a Javascript function

<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">
<!--
function doingThings()
{
// post back to raise the button's click event
<%= ClientScript.GetPostBackEventReference(Button2, string.Empty) %>
<asp:Button ID="Button1" runat="server" Text="Button1" OnClientClick="doingThings();return false"/>
<asp:Button ID="Button2" runat="server" Text="Button2" />
}
//-->
</SCRIPT>

This code will cancel the Button1 Click event and the postback is caused by Button2

And below function will return the Button which raised postback. You will see it is Button2

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
            Label4.Text = DirectCast(GetPostBackControl(Me.Page), Button).ID
    End If
 End Sub

    Public Shared Function GetPostBackControl(ByVal page As System.Web.UI.Page) As System.Web.UI.Control
        Dim control As Control = Nothing
        Dim ctrlname As String = page.Request.Params("__EVENTTARGET")
        If ctrlname IsNot Nothing AndAlso ctrlname <> [String].Empty Then
            control = page.FindControl(ctrlname)
        Else
            ' if __EVENTTARGET is null, the control is a button type and we need to 
            ' iterate over the form collection to find it 
            Dim ctrlStr As String = [String].Empty
            Dim c As Control = Nothing
            For Each ctl As String In page.Request.Form
                If ctl Is Nothing Then Continue For
                ' handle ImageButton controls ... 
                If ctl.EndsWith(".x") OrElse ctl.EndsWith(".y") Then
                    ctrlStr = ctl.Substring(0, ctl.Length - 2)
                    c = page.FindControl(ctrlStr)
                Else
                    c = page.FindControl(ctl)
                End If
                If TypeOf c Is System.Web.UI.WebControls.Button OrElse TypeOf c Is System.Web.UI.WebControls.ImageButton Then
                    control = c
                    Exit For
                End If
            Next
        End If
        Return control
    End Function
Advertisements