locked
handle event in formview RRS feed

  • Question

  • User-1047007741 posted

    hi, i have a page which has a formview. I need to handle an event in a child control in the edit and insert templates. how can i do this: this is the situation: i have a control in a control. The parent control is on the formview.

    This is the code:

    child control:(contains a dropdownlist)

    public event EventHandler selectionChangedEvent;

    public void DropDownListSchoolUse_SelectedIndexChanged(object sender, EventArgs e)

    {

    if (selectionChangedEvent != null)

    {

    selectionChangedEvent(sender, e);

    }

    }

     

    The parent control (which contains teh above child control)

    public event EventHandler selectionChangedEvent;

    protected void Page_Load(object sender, EventArgs e)
    {SchoolUseDropDownList.selectionChangedEvent += new EventHandler(SchoolUseDropDownListControl_selectionChangedEvent);}public void SchoolUseDropDownListControl_selectionChangedEvent(object sender, EventArgs e)

    {

    if (selectionChangedEvent != null)

    {

    selectionChangedEvent(sender, e);

    }

    }

     

    The Formview Page: (contains the Parent control above)

    public event EventHandler selectionChangedEvent;

    public SchoolDetailsEdit SchoolDetails;

    if (FormViewSchool.CurrentMode == FormViewMode.Edit)

    {

    SchoolDetails = (SchoolDetailsEdit)this.FormViewSchool.FindControl("SchoolDetailsEdit");

    SchoolDetails.selectionChangedEvent += new EventHandler(SchoolDetailsEditControl_selectionChangedEvent); ----->this line returns null for the event....it doesnt fire the SchoolDetailsEditControl_selectionChangedEvent below-----

    }

    public void SchoolDetailsEditControl_selectionChangedEvent(object sender, EventArgs e)

    {

    RequireTerms(); ---->this method just sets a value to true/false on another control.....but this method isnt being called when the event occurs

    }

     

    basically, i want the event to be handled in the formview, but the event is fired in the child control, which is contained in the parent control, which is contained in the formview.....can anyone spot anything wrong above? thanks

    Monday, May 19, 2008 7:19 AM

Answers

  • User1119078696 posted

    You really should be using a event delegate this will help you track your event easier.  Here is an overly simple expample of how event should be captured.  You are taking the right approach and subscribing to the event in each of your classes.  Is the parent control capturing the event.  Here's a way I know that works.

     Let's start with the delegate.  A delegate is a way of defining the signature of method you want to call when the event is triggered.  For example take this code:  (I always store this code in it's own cs file.  It is not contained in a class.  It just sits on its own.)

    public delegate void ButtonEventDelegate(object source, System.EventArgs e);

    Above we have declared a delegate called ButtonEventDelegate that will take two arguments.  When you create your method that you want to call when the event is triggered, it must have those two arguments.  The signatures must match.

    Next in your form (class) you declare an event and attach it to that delegate.  This is declared in a class refered to as the event publisher.

    public event ButtonEventDelegate ButtonClick;

     

    Now, here we go.  Your button will subscribe to ButtonClick by doing this.  This is called the subscriber

    myCommandButton.Click += MyCalledMethod;

    When your button is clicked it will call MyCalledMethod.  Of cource be for it can compile or call that method we have to create it.   "Do any of you methods match the signature of the delegate?" (childish but i think you get the point)

    private void MyCalledMethod(object sender, EventArgs e)
    {
       if (ButtonClicked != null)
       {
          
    ButtonClicked(sender, e);
      
    }
    }

    Please note the parameters in MyCalledMethod match the delegate (very important) and you see a little logic in there.  The event is only fired if something subscribed to it

    Those are the main pieces.  Delegate events are of course most useful between classes so make sure you include a reference to the class containing the event declareation (public event ButtonEventDelegate ButtonClick;) in your class that will fire the event and put the method that matches the signature.

    Ok here's the big example:

    In subscriber class:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 22, 2008 6:11 PM