自定义控件事件未在用户控件内触发 [英] Custom Control Event not firing inside a user control

查看:64
本文介绍了自定义控件事件未在用户控件内触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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





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



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





和我在usercontrol中的html代码:



and my html code in usercontrol:

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





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

1-我的代码出了什么问题?如何解决这个问题?

2-如果我想将自定义控件添加到更新面板并且我想要回复异步帖子,我必须做什么?

谢谢你,最好的问候



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

推荐答案

我发现了问题!当我们使用母版页时,客户端ID和控件的唯一ID是不同的,我们必须使用唯一的ID而不是客户端ID:



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


这篇关于自定义控件事件未在用户控件内触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆