Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I implement a custom control that handle post back event:
C#
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:
ASP.NET
<input type="button" value="Insert"  önclick="CallPostBack('1|2014/06/12')" />
                <CalendarControls:CalendarPostback ID="ctl1"  runat="server" ClientIDMode="Static"  önCalendarPostBack="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
Posted

1 solution

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:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900