Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Which control fired the postback process

Back to Forum: Tech Off
  • User profile image
    StretchMan

    In ASP.NET I would like to know if there a way to, in the Page_Load event, using the sender parameter (system.object) to know which control fired the postback ?

    Thanks

  • User profile image
    PeterF

    Hi

    Cast it to Control and then ask for it's name?
    Sorry, I'm about to go home from work, will check on it later if you didn't got the answer by then.

    Peter

    Ps. C# or VB.NET?

  • User profile image
    Rossj

    StretchMan wrote:
    In ASP.NET I would like to know if there a way to, in the Page_Load event, using the sender parameter (system.object) to know which control fired the postback ?


    Isn't the sender in this case whatever your Page derived class is.
    Why not use the events to work out when your button was pressed (or drop down item selected or whatever)  instead?  No need to put everything into your Page_Load method.

  • User profile image
    StretchMan

    Rossj wrote:


    Isn't the sender in this case whatever your Page derived class is.
    Why not use the events to work out when your button was pressed (or drop down item selected or whatever)  instead?  No need to put everything into your Page_Load method.


    You are correct Rossj, it's more of a theorical question, to understand more the framework. The sender value is the object of the derived page object. With that... is there a way to know the control that fired that ? I dont want and should not put everything in my page_load event... aggree at 100%

  • User profile image
    Rossj

    I'm not really much of a UI person but I was under the impression that your Page_Load will get called just before the event handler for whichever control fired the postback (which is the one you want to know about).  Beyond that I dunno - sorry maybe if you could find out where the Page_Load delegate is added?

    I have seen the Page_Load event causing problems in the past, specifically one of our developers populating a drop down in Page_Load and then when the SelectedItemChanged fired it was always the same value (item 0) or not set at all.  All because it was not wrapped in a if ( ! IsPostBack ), and to me (with my limited knowledge) that looks like a flaw in the whole process - it breaks the flow of the system when to all intents and purposes you *could* (but maybe shouldn't) treat webforms like winforms.

    Edit: Silly me, I found it
        private void InitializeComponent()
            {   
                this.Load += new System.EventHandler(this.Page_Load);
            }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.