Event handling in custom control
-
I hope someone can help with this problem. I have a custom control with a submit button on it. I simply want to wire up the OnClick event for the button, but it never seems to get called. I have checked all the documentation and I think I am doing everything correctly. But obviously something is wrong. In my custom control, I override CreateChildControls and add an OnClick function as follows:
protected override void CreateChildControls() { Button btn = new Button(); btn.Text = "Submit"; btn.Click += new System.EventHandler(OnClick); Controls.Add(btn); } public void OnClick(object o, EventArgs e) { ... }
And create the custom control in my web form as follows:<%@ Register TagPrefix="RI" Namespace="CustomControl" Assembly="CustomControl" %> ... <RI:WebCustomControl1 runat="server" ID="Webcustomcontrol11" NAME="Webcustomcontrol11" />
But OnClick never gets called. Can anyone help??????? I am totally at a loss as to why OnClick is not called. Many thanks! -
I hope someone can help with this problem. I have a custom control with a submit button on it. I simply want to wire up the OnClick event for the button, but it never seems to get called. I have checked all the documentation and I think I am doing everything correctly. But obviously something is wrong. In my custom control, I override CreateChildControls and add an OnClick function as follows:
protected override void CreateChildControls() { Button btn = new Button(); btn.Text = "Submit"; btn.Click += new System.EventHandler(OnClick); Controls.Add(btn); } public void OnClick(object o, EventArgs e) { ... }
And create the custom control in my web form as follows:<%@ Register TagPrefix="RI" Namespace="CustomControl" Assembly="CustomControl" %> ... <RI:WebCustomControl1 runat="server" ID="Webcustomcontrol11" NAME="Webcustomcontrol11" />
But OnClick never gets called. Can anyone help??????? I am totally at a loss as to why OnClick is not called. Many thanks!I'm not very sure but did you inherit your control from
INamingContainer
? Its is needed dor handling child control events. Mazy "And the carpet needs a haircut, and the spotlight looks like a prison break And the telephone's out of cigarettes, and the balcony is on the make And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits -
I'm not very sure but did you inherit your control from
INamingContainer
? Its is needed dor handling child control events. Mazy "And the carpet needs a haircut, and the spotlight looks like a prison break And the telephone's out of cigarettes, and the balcony is on the make And the piano has been drinking, the piano has been drinking...not me...not me-Tom WaitsThanks very much, Mazdak. That was exactly my problem. I was not inheriting from INamingContainer. I didn't realise you had to. Mike.