all groups > dotnet faqs > november 2003 >
dotnet faqs :
vb.net - event handler
In the VB.NET Page_Load() function , how can I know which control causes the page postback?
Your assumption is correct. However, when the control's Event Handler set class's field or property, it is supposed to be AFTER the Page_Load function is called during a postback, isn't it? So in Page_Load function, we still don't know which control causes the PostBack. Thanks. [quoted text, click to view] "Kevin Spencer" <kevin@takempis.com> wrote in message news:#2gLRpHsDHA.2492@TK2MSFTNGP12.phx.gbl... > Assuming that the Control caused a PostBack because of an event, you should > know by which Event Handler is called. For example, you could have the Event > Handler for each Control set a field or property in your class. > > -- > Kevin Spencer > .Net Developer > Microsoft MVP > Big things are made up > of lots of little things. > > "Bob" <bobjiang@yahoo.com> wrote in message > news:OVFZ85GsDHA.2004@TK2MSFTNGP10.phx.gbl... > > In the VB.NET Page_Load() function , how can I know which control causes > the > > page postback? > > > > > > > > > >
Assuming that the Control caused a PostBack because of an event, you should know by which Event Handler is called. For example, you could have the Event Handler for each Control set a field or property in your class. -- Kevin Spencer ..Net Developer Microsoft MVP Big things are made up of lots of little things. [quoted text, click to view] "Bob" <bobjiang@yahoo.com> wrote in message news:OVFZ85GsDHA.2004@TK2MSFTNGP10.phx.gbl... > In the VB.NET Page_Load() function , how can I know which control causes the > page postback? > > > >
I don't think that is available when the page returns from the postback. Keeping a simple var should do the trick though. Track it your self. [quoted text, click to view] "Bob" <bobjiang@yahoo.com> wrote in message news:#wror3HsDHA.4092@tk2msftngp13.phx.gbl... > Your assumption is correct. However, when the control's Event Handler set > class's field or property, it is supposed to be AFTER the Page_Load function > is called during a postback, isn't it? So in Page_Load function, we still > don't know which control causes the PostBack. > > Thanks. > > "Kevin Spencer" <kevin@takempis.com> wrote in message > news:#2gLRpHsDHA.2492@TK2MSFTNGP12.phx.gbl... > > Assuming that the Control caused a PostBack because of an event, you > should > > know by which Event Handler is called. For example, you could have the > Event > > Handler for each Control set a field or property in your class. > > > > -- > > Kevin Spencer > > .Net Developer > > Microsoft MVP > > Big things are made up > > of lots of little things. > > > > "Bob" <bobjiang@yahoo.com> wrote in message > > news:OVFZ85GsDHA.2004@TK2MSFTNGP10.phx.gbl... > > > In the VB.NET Page_Load() function , how can I know which control causes > > the > > > page postback? > > > > > > > > > > > > > > > > > >
Well, you didn't specify WHEN you wanted to get the data. If you need to find out before the event is fired and handled, you can check Request.Form("__EVENTTARGET") - this will have the ID of the control which caused the PostBack in it. -- Kevin Spencer ..Net Developer Microsoft MVP Big things are made up of lots of little things. [quoted text, click to view] "Bob" <bobjiang@yahoo.com> wrote in message news:#wror3HsDHA.4092@tk2msftngp13.phx.gbl... > Your assumption is correct. However, when the control's Event Handler set > class's field or property, it is supposed to be AFTER the Page_Load function > is called during a postback, isn't it? So in Page_Load function, we still > don't know which control causes the PostBack. > > Thanks. > > "Kevin Spencer" <kevin@takempis.com> wrote in message > news:#2gLRpHsDHA.2492@TK2MSFTNGP12.phx.gbl... > > Assuming that the Control caused a PostBack because of an event, you > should > > know by which Event Handler is called. For example, you could have the > Event > > Handler for each Control set a field or property in your class. > > > > -- > > Kevin Spencer > > .Net Developer > > Microsoft MVP > > Big things are made up > > of lots of little things. > > > > "Bob" <bobjiang@yahoo.com> wrote in message > > news:OVFZ85GsDHA.2004@TK2MSFTNGP10.phx.gbl... > > > In the VB.NET Page_Load() function , how can I know which control causes > > the > > > page postback? > > > > > > > > > > > > > > > > > >
Hi Ray, Keeping it simple is what I thought also. Why would you need that if you have a "not ispostback" and can catch all events with the same structure if you want together in one sub" But tracking it yourself is in my idea impossible (if there is more than one control on a page), the idea of an "event" is that you cannot track it in the future. It happens mostly because the user fires it. I find the mechanisme to catch that well done made. Just a thought, Cor [quoted text, click to view] > I don't think that is available when the page returns from the postback. > Keeping a simple var should do the trick though. Track it your self.
Hello, For web controls which can invoke __doPostBack. At the page load, I supposedly can know who caused the PostBack by checking Request.Form("__EVENTTARGET"). It does work for the dropdownlist, however it doesn't work for the button. When the button is clicked, on page load, Request.Form("__EVENTTARGET") ="". If I call Page.RegisterHiddenField("__EVENTTARGET", "FakeButton"), then when the button is clicked, Request.Form("__EVENTTARGET") ="FakeButton". It looks like when the button on the page is clicked, ASP.NET does NOT ignore the hidden field in favor of the actual button click. Is there something I missed? Thanks for your input. Here is the code snippets. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Page.RegisterHiddenField("__EVENTTARGET", "FakeButton") If Request.Form("__EVENTTARGET") = "Button1" Then 'do one sth ElseIf Request.Form("__EVENTTARGET") = "Button2" Then 'do another sth End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim b As Button = CType(sender, Button) Label1.Text = "You clicked " & b.ID End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Label1.Text = "You clicked " & sender.ID End Sub
Why not just check the "sender" event argument of the Page_Load event? This is what it is there for. Just check sender.getType.toString and then you'll know what kind of control caused the postback. Then you could cast sender into that type and check its name. [quoted text, click to view] "Bob" <bobjiang@yahoo.com> wrote in message news:ebrJCpssDHA.2392@TK2MSFTNGP10.phx.gbl... > Hello, > > For web controls which can invoke __doPostBack. At the page load, I > supposedly can know who caused the PostBack by checking > Request.Form("__EVENTTARGET"). It does work for the dropdownlist, however > it doesn't work for the button. When the button is clicked, on page load, > Request.Form("__EVENTTARGET") ="". If I call > Page.RegisterHiddenField("__EVENTTARGET", "FakeButton"), then when the > button is clicked, Request.Form("__EVENTTARGET") ="FakeButton". It looks > like when the button on the page is clicked, ASP.NET does NOT ignore the > hidden field in favor of the actual button click. > > Is there something I missed? Thanks for your input. > > Here is the code snippets. > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > 'Put user code to initialize the page here > Page.RegisterHiddenField("__EVENTTARGET", "FakeButton") > > If Request.Form("__EVENTTARGET") = "Button1" Then > 'do one sth > ElseIf Request.Form("__EVENTTARGET") = "Button2" Then > 'do another sth > End If > > End Sub > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click > Dim b As Button = CType(sender, Button) > Label1.Text = "You clicked " & b.ID > End Sub > > Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button2.Click > Label1.Text = "You clicked " & sender.ID > End Sub > >
That is also what I got in webform. SO back to the question. How can I know which button invoked the button event? [quoted text, click to view] "Scott M." <s-mar@badspamsnet.net> wrote in message news:#uKU9i2sDHA.2348@TK2MSFTNGP09.phx.gbl... > No, you are right Cor. I have used sender in Windows Forms projects without > a problem and just assumed that it would work the same way in on a Web Form. > > Thanks. > > "Cor" <non@non.com> wrote in message > news:%2307UqKzsDHA.2448@TK2MSFTNGP09.phx.gbl... > > Hi Scott, > > > > While I did not check all the other methods, because I think it becomes a > > lot of rubish in the load event, while there is a good methode, that only > > needs three lines. > > In VB.net as example that is > > \\\ > > sub from page control event > > todo > > end sub > > /// > > > > Did I test your sample because I could not believe it, but you never > knows. > > (getting the type of sender is in webforms not that simple as in > > windowforms) > > > > I did try it with only one serverside button on the page > > \\\ > > Private Sub Page_Load(ByVal sender As System.Object, _ > > ByVal e As System.EventArgs) Handles MyBase.Load > > Dim a As String = sender.GetType.ToString > > 'returns always "asp.webform1.aspx" > > End Sub > > /// > > > > When I did understand something wrong let me know? > > > > Cor > > > > > Why not just check the "sender" event argument of the Page_Load event? > > This > > > is what it is there for. Just check sender.getType.toString and then > > you'll > > > know what kind of control caused the postback. Then you could cast > sender > > > into that type and check its name. > > > > > > > > >
Hi Scott, While I did not check all the other methods, because I think it becomes a lot of rubish in the load event, while there is a good methode, that only needs three lines. In VB.net as example that is \\\ sub from page control event todo end sub /// Did I test your sample because I could not believe it, but you never knows. (getting the type of sender is in webforms not that simple as in windowforms) I did try it with only one serverside button on the page \\\ Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim a As String = sender.GetType.ToString 'returns always "asp.webform1.aspx" End Sub /// When I did understand something wrong let me know? Cor [quoted text, click to view] > Why not just check the "sender" event argument of the Page_Load event? This > is what it is there for. Just check sender.getType.toString and then you'll > know what kind of control caused the postback. Then you could cast sender > into that type and check its name. >
No, you are right Cor. I have used sender in Windows Forms projects without a problem and just assumed that it would work the same way in on a Web Form. Thanks. [quoted text, click to view] "Cor" <non@non.com> wrote in message news:%2307UqKzsDHA.2448@TK2MSFTNGP09.phx.gbl... > Hi Scott, > > While I did not check all the other methods, because I think it becomes a > lot of rubish in the load event, while there is a good methode, that only > needs three lines. > In VB.net as example that is > \\\ > sub from page control event > todo > end sub > /// > > Did I test your sample because I could not believe it, but you never knows. > (getting the type of sender is in webforms not that simple as in > windowforms) > > I did try it with only one serverside button on the page > \\\ > Private Sub Page_Load(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles MyBase.Load > Dim a As String = sender.GetType.ToString > 'returns always "asp.webform1.aspx" > End Sub > /// > > When I did understand something wrong let me know? > > Cor > > > Why not just check the "sender" event argument of the Page_Load event? > This > > is what it is there for. Just check sender.getType.toString and then > you'll > > know what kind of control caused the postback. Then you could cast sender > > into that type and check its name. > > > >
You could add a custom value to VIEWSTATE that differs depending on what control was interacted with: ViewState.Add("EventStartedBy", controlName) You could then check the "EventStartedBy" key in ViewState on the PostBack via a Select statement to see what its value is. I'm sure there's got to be another way, but this should work. [quoted text, click to view] "Bob" <bobjiang@yahoo.com> wrote in message news:OTA79n2sDHA.2004@TK2MSFTNGP10.phx.gbl... > That is also what I got in webform. SO back to the question. How can I know > which button invoked the button event? > > "Scott M." <s-mar@badspamsnet.net> wrote in message > news:#uKU9i2sDHA.2348@TK2MSFTNGP09.phx.gbl... > > No, you are right Cor. I have used sender in Windows Forms projects > without > > a problem and just assumed that it would work the same way in on a Web > Form. > > > > Thanks. > > > > "Cor" <non@non.com> wrote in message > > news:%2307UqKzsDHA.2448@TK2MSFTNGP09.phx.gbl... > > > Hi Scott, > > > > > > While I did not check all the other methods, because I think it becomes > a > > > lot of rubish in the load event, while there is a good methode, that > only > > > needs three lines. > > > In VB.net as example that is > > > \\\ > > > sub from page control event > > > todo > > > end sub > > > /// > > > > > > Did I test your sample because I could not believe it, but you never > > knows. > > > (getting the type of sender is in webforms not that simple as in > > > windowforms) > > > > > > I did try it with only one serverside button on the page > > > \\\ > > > Private Sub Page_Load(ByVal sender As System.Object, _ > > > ByVal e As System.EventArgs) Handles MyBase.Load > > > Dim a As String = sender.GetType.ToString > > > 'returns always "asp.webform1.aspx" > > > End Sub > > > /// > > > > > > When I did understand something wrong let me know? > > > > > > Cor > > > > > > > Why not just check the "sender" event argument of the Page_Load event? > > > This > > > > is what it is there for. Just check sender.getType.toString and then > > > you'll > > > > know what kind of control caused the postback. Then you could cast > > sender > > > > into that type and check its name. > > > > > > > > > > > > > > > >
Hi Bob, Do you want to know what button did do the post back without to test them all, or do you want absolute to test them in the load event. For the first you can use dynamically made buttons and test it only once (and I have made an example if you want) for the second I have no solution and I think that will absolute become a big hash in the load event (I was also thinking like Scott on the viewstate for that). Cor
Don't see what you're looking for? Try a search.
|
|
|