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
LVL 8
rodmjayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Solar_FlareCommented:
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
0
rodmjayAuthor Commented:
Well the sender does not have a name attribute, but the ToString method returns the name of the page
0
praneethaCommented:
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 { }
        }
    }
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

praneethaCommented:
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
0
appariCommented:
try this code from http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GavinMannionCommented:
Why are you not just using the buttons event handler?

It will then automatically go into the correct Event Handler....
0
raterusCommented:
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.
0
rodmjayAuthor Commented:
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.
0
GavinMannionCommented:
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...

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.