自定义控件加事件

作用:当自定义控件的下拉发生变化时,其他控件或HTML代码做相应的业务处理。 

一、自定义控件的HTML代码:

.....................

.....

<asp:DropDownList ID="ddl_ProfileType" Width="200px" runat="server" AutoPostBack="true"
                   OnSelectedIndexChanged="ddl_ProfileType_SelectedIndexChanged">
</asp:DropDownList>

......

..................

当下拉发生变化时,触发一个事件。

 

二、自定义控件的CS代码:

public partial class AddProfile : System.Web.UI.UserControl
{
 public event EventHandler ChangeProfileType;
 private void OnChangeProfileType(object sender,EventArgs e)
 {
  if (this.ChangeProfileType != null)
  {
   ChangeProfileType(this, e);
  }
 }
 
 protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
  {

  }
 }
 //機構類型
 protected void ddl_ProfileType_SelectedIndexChanged(object sender, EventArgs e)
 {
  if (this.ddl_ProfileType.SelectedValue == ((int)ProfileType.Individual).ToString())
  {
   Create_ProfileTypeShow(true);
  }
  else
  {
   Create_ProfileTypeShow(false);
  }
  this.OnChangeProfileType(sender, e);
 }
}

 

三、调用自定义控件的代码

public partial class Profile_Add : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
  {
  }
  this.AddProfile1.ChangeProfileType += new EventHandler(AddProfile1_OnChangeProfileType);
 }


 void AddProfile1_OnChangeProfileType(object sender, EventArgs e)
 {
  ProfileORM ProfileModel = new ProfileORM();
  ProfileModel = this.AddProfile1.GetProfileModel();
  if (ProfileModel.ProfileType == (int)ProfileType.Individual)
  {
   this.tr_MediaIndivudual.Visible = true;
   this.tr_MediaType.Visible = false;
  }
  else
  {
   this.tr_MediaIndivudual.Visible = false;
   this.tr_MediaType.Visible = true;
  }
 }
}

 

注意:如果自定义控件放在Ajax里面,其他代码也要放在Ajax里面,才能看得出效果。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值