简体   繁体   English

自定义控件事件未在用户控件内触发

[英]Custom Control Event not firing inside a user control

I implement a custom control that handle post back event: 我实现了一个用于处理回发事件的自定义控件:

namespace CalendarControls
{
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]  
    public class CalendarPostback : WebControl, IPostBackEventHandler
    {
        public delegate void CalendarPostBackHandler(object sender, CalendarPostbackEventArgs e);
        public event CalendarPostBackHandler CalendarPostBack;

        public CalendarPostback()
        {

        }

        #region IPostBackEventHandler Members

        public void RaisePostBackEvent(string eventArgument)
        {
            string[] values = eventArgument.Split('|');

            CalendarPostbackEventArgs args = new CalendarPostbackEventArgs();
            args.EventType = (eEventType)Convert.ToInt32(values[0]);

            if (args.EventType == eEventType.Insert)
                args.EventDate = Convert.ToDateTime(values[1]);
            else
                args.EventId = Convert.ToInt32(values[1]);

            if (CalendarPostBack != null)
                CalendarPostBack(this, args);
        }

        #endregion
    }

    public class CalendarPostbackEventArgs : EventArgs
    {
        public CalendarPostbackEventArgs()
        {

        }

        public eEventType EventType
        {
            get;
            set;
        }

        public DateTime EventDate
        {
            get;
            set;
        }

        public int? EventId
        {
            get;
            set;
        }
    }
}

and I use this custom control in a usercontrol ,and call it on a button click inside my usercontrol with following javascript code: 然后在用户控件中使用此自定义控件,然后在用户控件中单击以下按钮,将其调用,并使用以下JavaScript代码:

function CallPostBack(eventArguments) {
    __doPostBack('<%=ctl1.ClientID %>', eventArguments);
}

and my html code in usercontrol: 和我在用户控件中的html代码:

<input type="button" value="Insert" onclick="CallPostBack('1|2014/06/12')" />
                <CalendarControls:CalendarPostback ID="ctl1" runat="server" ClientIDMode="Static" OnCalendarPostBack="ctl1_CalendarPostBack" />

but when I click on my button, page postback occur but ctl1_CalendarPostBack event not fired. 但是,当我单击我的按钮时,会发生页面回发,但不会触发ctl1_CalendarPostBack事件。 also I must to say that I add user control directly to aspx page (not dynamically) and my aspx page have a master page. 我还必须说我直接将用户控件添加到aspx页面(不是动态添加),并且aspx页面具有母版页。 now I have two question: 现在我有两个问题:

  1. what's wrong with my code? 我的代码有什么问题? how to solve this issue? 如何解决这个问题?
  2. if I want to add my custom control to an update panel and I want to have an async post back, what I must do? 如果我想将自定义控件添加到更新面板中,并且想要异步回发,该怎么办?

thank you, best regards 谢谢您最好的问候

I found problem! 我发现问题了! when we use master page ,client id and unique id of control is different and we must to use unique id instead of client id: 当我们使用母版页时,客户端ID和控件的唯一ID是不同的,我们必须使用唯一ID而不是客户端ID:

   function CallPostBack(eventArguments) {
    __doPostBack('<%=ctl1.UniqueID %>', eventArguments);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM