Link to home
Start Free TrialLog in
Avatar of rodmjay
rodmjayFlag for United States of America

asked on

Which button caused a postback

I am using asp.net 2.0 there does not appear to be an __Eventtarget control.  How can i get the reference to the button pressed?    

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            Response.Write(Request.Form("__EVENTTARGET"))

            'If Request.Form(btnSubmit.UniqueID).Length > 0 Then
            '    Response.Write("graded")
            'End If
            'If Request.Form(btnReset.UniqueID).Length > 0 Then
            '    Response.Write("reset")
            'End If
        End If
    End Sub
Avatar of Solar_Flare
Solar_Flare

i beleive the sender object is the control that caused the postback.

try adding in something like Response.Write(Sender.Name)  and see what you get
Avatar of rodmjay

ASKER

Well the sender does not have a name attribute, but the ToString method returns the name of the page
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Response.Write(sender.ToString()+"<br>");

            try
            {
                Button obj = (Button)sender;
                Response.Write(obj.ID+"<br>") ;
            }
            catch { }
        }
    }
u have to typecast it to button and then get the id of it.

and in case button did not cause the post back its gonna throw the exception which you need to neglect or do something  whatever you want to
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why are you not just using the buttons event handler?

It will then automatically go into the correct Event Handler....
lol, THANK YOU Gavin, it's amazing how some "experts" will help someone walk right off the cliff.  Obviously it should not be this difficult to know which button was pressed.
Avatar of rodmjay

ASKER

Obviously, but there was some code ran in the page_load that was dependant on what button was pressed.  I needed to know before the button_Click event would normally fire.
Glad you found a solution but generally I would recommend just then moving the required code into it's own function and calling it from the button_click event.

My preference is to have an absolute minimum of code in the page_load event...