locked
Sequence execution of control's event and custom control's event RRS feed

  • Question

  • User-218479044 posted

    Hi,

    I have created a custom Button (called BtCustom) that has a TargetControl to a TextBox. On OnLoad event of BtCustom , i register event handler Text Changed of TextBox

    The Code on the OnLoad event of BtCustom and TargetControl Text Changed as follows:

            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                TargetControl.TextChanged += new EventHandler(TargetControl_TextChanged);
                TargetControl.AutoPostBack = true;
            }
            protected void TargetControl_TextChanged(object sender, EventArgs e)
            {
                // do something here (a)
            }


    On the webform, I add a TextBox (called Txt1) then a BtCustom and set TargetControl to Txt1. I run and test it, it's working as expected.

    Now, back on the webform, i set OnTextChanged to Txt1 and the code as follows :

    protected void Txt1_TextChanged(object sender, EventArgs e)

    {

       // do something here (b)

    }

    And on debugging, i found the sequence of execution will be Txt1_TextChanged first then TargetControl_TextChanged.

    Now, how do i tell the program at webform :

    - to execute TargetControl_TextChanged first then Txt1_TextChanged

    - Not to execute TargetControl_TextChanged

    Thanks,

    Andy

    Note : I have tried to used base.OnTextChanged, however, i can't find OnTextChanged after i type base.

    Wednesday, July 13, 2011 12:40 AM

Answers

  • User-16411453 posted

    I would go for a autocomplete type of event, in which as there typing in the box, the data changes. I can't see relying on asp.net and server side processing for a task like that. I would shift it over to javascript on the client side, in which you call a web service to grab the new data, and javascript to populate the space.

    no example code here, just a suggestion.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 31, 2011 1:07 AM

All replies

  • User3866881 posted

    to execute TargetControl_TextChanged first then Txt1_TextChanged

    As far as I see, To do this directly is hard. But we can have a trick to do with that.

    1) Set a flag (boolean) in the class, and its initizalization value is false.

    2) in the TargetControl_TextChanged, please set it to true, and in the  TargetControl_TextChanged event, please raise the Txt1_TextChanged event.

    3) in the Txt1_TextChanged , you should say:

    if (flag)

    {

       //Do something ……

    }

    Not to execute TargetControl_TextChanged

    To this problem, I think remove the event handler of TargetControl_TextChanged. Or just set a public property where there's a ViewState to contain a boolean value, which is used to indicate whether TargetControl_TextChanged is executed or not. Something looks like this——

    if(PublicProperty)

    {

        //Do what you like……

    }

    Thursday, July 14, 2011 9:28 PM
  • User-218479044 posted

    Thanks Decker Dong for your help.

    Point no. 2, the workaround should work.

    Point no. 1, still can't get it. Both TargetControl_TextChanged and Txt1_TextChanged is actually the same control. What make them difference is, TargetControl_TextChanged declared through class, while Txt1_TextChanged through Webforms. Can you give me example?

    Friday, July 15, 2011 1:15 AM
  • User-16411453 posted

    I would go for a autocomplete type of event, in which as there typing in the box, the data changes. I can't see relying on asp.net and server side processing for a task like that. I would shift it over to javascript on the client side, in which you call a web service to grab the new data, and javascript to populate the space.

    no example code here, just a suggestion.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 31, 2011 1:07 AM