?
Solved

Which button caused a postback

Posted on 2006-04-12
9
Medium Priority
?
662 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:rodmjay
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 16440155
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
 
LVL 8

Author Comment

by:rodmjay
ID: 16440191
Well the sender does not have a name attribute, but the ToString method returns the name of the page
0
 
LVL 15

Expert Comment

by:praneetha
ID: 16441357
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:praneetha
ID: 16441366
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
 
LVL 39

Accepted Solution

by:
appari earned 1500 total points
ID: 16441863
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
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16443404
Why are you not just using the buttons event handler?

It will then automatically go into the correct Event Handler....
0
 
LVL 33

Expert Comment

by:raterus
ID: 16445045
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
 
LVL 8

Author Comment

by:rodmjay
ID: 16454335
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
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16459027
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question