用于自定义控件的onclientclick事件

时间:2014-10-30 07:31:24

标签: asp.net c#-4.0 custom-controls

我有一个div,里面有自定义控件

<div class="modal modal-medium" id="modal-default-email">
            <asp:DefaultMailMessage ID="DefaultMailMessageID" runat="server"   />
            <!-- /.modal-content -->
        </div>

我只想创建一个客户端点击事件。怎么样?分享您的想法......

1 个答案:

答案 0 :(得分:0)

您必须在控件中声明事件,为句柄回发事件实施IPostBackEventHandler并注册GetPostBackEventReference(this, "Click") clint-side,例如

因此或多或少

 using System.Web.UI.HtmlControls;

 public class MyControl : CompositeControl, IPostBackEventHandler
    {
        //Declare event
        public event EventHandler Click;

        protected override void CreateChildControls()
        {
            //Creante your control 
            HtmlGenericControl div = new HtmlGenericControl("div");

            //set your property

            //Set postback reference client-side
            div.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, "Click"));
         //Add control
         Controls.Add(div);
        }

        protected virtual void OnClick(EventArgs e)
        {
            if (((this.Click != null)))
            {
                this.Click.Invoke(this, e);
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            if ((object.ReferenceEquals(eventArgument, "Click")))
            {
                this.OnClick(EventArgs.Empty);
            }
        }
    }

您还会看到http://msdn.microsoft.com/en-us/library/aa719907(v=vs.71).aspx