【问题标题】:Custom Control Event not firing inside a user control自定义控件事件未在用户控件内触发
【发布时间】:2014-06-14 19:53:21
【问题描述】:

我实现了一个处理回发事件的自定义控件:

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;
        }
    }
}

我在用户控件中使用此自定义控件,并使用以下 javascript 代码在我的用户控件内单击按钮调用它:

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

和我在用户控件中的 html 代码:

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

但是当我点击我的按钮时,页面回发发生但 ctl1_CalendarPostBack 事件未触发。我还必须说,我将用户控件直接添加到 aspx 页面(不是动态地),并且我的 aspx 页面有一个母版页。 现在我有两个问题:

  1. 我的代码有什么问题?如何解决这个问题?
  2. 如果我想将我的自定义控件添加到更新面板并且我想要异步回发,我必须做什么?

谢谢你, 最好的问候

【问题讨论】:

    标签: asp.net user-controls event-handling custom-controls


    【解决方案1】:

    我发现了问题!当我们使用master page时,client id和control的unique id是不同的,我们必须使用unique id而不是client id:

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

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      相关资源
      最近更新 更多