问题描述
如题。代码如下,也希望能对初学者起到一些启发意义,并请各位指点:publicclassUTextBox:TextBox{//在类里,定义一个委托publicdelegatevoidMyHandler(objectsender,EventArgse);//定义事件的KEYprivatestaticreadonlyobjectMyObj=newobject();//使用这个委托,定义事件publiceventMyHandlerMyEvent{add{Events.AddHandler(MyObj,value);}remove{Events.RemoveHandler(MyObj,value);}}//使用事件的方法privatevoidOnMyMethodHandler(EventArgse){MyHandlerhandler=(MyHandler)Events[MyObj];if(handler!=null){handler(this,e);}}privatevoidMyClick(objectsender,EventArgse){OnMyMethodHandler(e);}protectedoverridevoidRender(System.Web.UI.HtmlTextWriterwriter){base.Render(writer);RenderChildren(writer);//此方法可以将嵌入的控件呈现出来}protectedoverridevoidCreateChildControls(){base.CreateChildControls();Buttonbtn=newButton();btn.ID="btn";btn.Text="Test";btn.Click+=newEventHandler(TextBox_Change);Controls.Add(btn);}}前台页面测试:<cc1:UTextBox2ID="UTextBox2"runat="server"onmyevent="UTextBox2_MyEvent"></cc1:UTextBox2>后台:protectedvoidUTextBox2_MyEvent(objectsender,EventArgse){Response.Write(1111111111111);}运行后,点击“Test”按钮,但并未显示111111111,只是页面刷新了一下。不知道我的事件哪里没写好,还是其他原因?
解决方案
解决方案二:
等待。。。
解决方案三:
路过,帮顶!!