0
votes

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:

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

and my html code in usercontrol:

<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. also I must to say that I add user control directly to aspx page (not dynamically) and my aspx page have a master page. 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

1

1 Answers

0
votes

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:

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