rodmjay
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.For m("__EVENT TARGET"))
'If Request.Form(btnSubmit.Uni queID).Len gth > 0 Then
' Response.Write("graded")
'End If
'If Request.Form(btnReset.Uniq ueID).Leng th > 0 Then
' Response.Write("reset")
'End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
Response.Write(Request.For
'If Request.Form(btnSubmit.Uni
' Response.Write("graded")
'End If
'If Request.Form(btnReset.Uniq
' Response.Write("reset")
'End If
End If
End Sub
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.ToSt ring()+"<b r>");
try
{
Button obj = (Button)sender;
Response.Write(obj.ID+"<br >") ;
}
catch { }
}
}
{
if (Page.IsPostBack)
{
Response.Write(sender.ToSt
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why are you not just using the buttons event handler?
It will then automatically go into the correct 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.
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...
My preference is to have an absolute minimum of code in the page_load event...
try adding in something like Response.Write(Sender.Name