根据调用表单自定义asp.net用户控件的事件处理

本文关键字:用户 控件 事件处理 net asp 调用 表单 自定义 | 更新日期: 2023-09-27 18:29:47

我使用的是两种不同aspx形式的asp.net用户控件。如何根据调用表单自定义用户控件的事件处理?

void ComboboxCountry_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
  {
            if it is form1 that called the User-control => do process 1
            if it is form2 that called the User-control => do process 2
  }

谢谢。

根据调用表单自定义asp.net用户控件的事件处理

尽管事实上,如果这是一个你正在尝试实现的好设计:你可以向你的控件添加一个属性,比如

public BehaviourEnum Behaviour { get; set; } // You need to implement the enum

然后你可以

void ComboboxCountry_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    if (Behaviour == BehaviourEnum.Behave1) // etc.
}

实现控件的页面需要相应地设置Behavior属性。

编辑:如果您的控件需要与父页交互,我会在父页上引入一个界面。然后你可以设计这样的东西:

// The page containing this control needs to implement IMasterpage
public IMasterpage Masterpage { get; set; } 
void ComboboxCountry_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    // Propagate the behavour to your parent page
    Masterpage.CallwatheverMethodInYourInterface();
}

目标是将依赖于父页面的行为传播到父页面本身。这样你就可以保持你的控制力和独立性。