[英]Add PreRender Event to User Control in asp.net
I am converting a vb user control to c#. 我正在将VB用户控件转换为C#。 I don't remember how to bind an event to a control in c#.
我不记得如何将事件绑定到c#中的控件。 In vb its pretty straight forward with the Handles clause:
在vb中,它的Handles子句非常简单:
Protected Sub Control_PreRender(Sender As Object, e As EventArgs) Handles Me.PreRender
I cannot seem to work out how to do this in c# short of just creating the method and hope it links itself. 除了创建方法并希望它自身链接之外,我似乎无法在c#中解决如何做到这一点。 Is this the way to go?
这是要走的路吗?
public void Control_PreRender(object sender, EventArgs e)
You got it almost right, the only problem is that events don't auto-attach themselves. 您几乎完全正确,唯一的问题是事件不会自动附加。 You need to add this to the constructor of the control:
您需要将此添加到控件的构造函数中:
public MyControl()
{
// your current code
PreRender += Control_PreRender;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.