Handle UserControl raised event (2): Raising Timing

Following the last post: Handle UserControl raised event. We will have a look at the timing of such event raised.
The UserControl event raise timing depends how the event is implemented in the UserControl.
In the last post, we have a UserControl event “TextboxTextChanged” which is raise by a TextChanged event. So the timing will be the same as the TextChanged event of the TextBoxt control. That is, when the page is posted back to the sever (in the case of the text in TextBox has been changed).
If we raise the UserControl event TextboxTextChanged in the Page_Load of the UserControl:
protected void Page_Load(object sender, EventArgs e)
{
TextboxTextChanged(sender, e);
}

Then the TextboxTextChanged will be raised when the in a different time. This means the UserControl event doesn’t have to raise in a Control event but can be anywhere. I.e. Let’s say in our UserControl, we have a event:
public event EventHandler CheckFailed;

And this event will raise in the function DoSomeCheck:

public void DoSomeCheck(bool con)
{
if (!con)
CheckFailed("Reason", new EventArgs());
}

The logic can be complex but here only is an example. The event only raise in the case of fail.

Then you can handle the event in the main page which contains that UserControl. Such pattern can be useful when the things you do in case of check fail are related to the main page but you still want the UserControl has that Check function.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章