Custom Control Event Handling Problemhttp://forums.asp.net/t/1119447.aspx/1?Custom+Control+Event+Handling+ProblemTue, 26 Jun 2007 11:15:53 -040011194471743621http://forums.asp.net/p/1119447/1743621.aspx/1?Custom+Control+Event+Handling+ProblemCustom Control Event Handling Problem <p>Hi,</p> <p>&nbsp;&nbsp; I'm novice and i would like to know that how can we handle the events like TextChanged event for the text box, OnClick Event for the button control, etc.</p> <p>Please help me</p> <p>Thanks in advance</p> 2007-06-07T12:12:02-04:001743793http://forums.asp.net/p/1119447/1743793.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>The short-cut.&nbsp;</p> <p>Double click a button on your page and you will automatically go to the onclick event handle for that button. You can then write code to be excecuted when that button is clicked. </p> <p>&nbsp;In general</p> <p>If you right click a control, let it be a button, textbox or dropdownlist or radiobuttonlist and select properties, you see a yellow &quot;thing&quot;, move the mouse over that yellow &quot;thing&quot; and you will see a tooltip saying 'events'. If you click it, you will see all events that can be handled&nbsp;for that control. So just double click&nbsp;in the column at the right of the event you want to handle and you will be right in its event handle and you can start writing your code there.</p> 2007-06-07T13:42:49-04:001748209http://forums.asp.net/p/1119447/1748209.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>hi,</p> <p>&nbsp; I've done this but when double clicking the custom control button I'm not getting the text changed event even though I'm getting in the Properties event field</p> <p>&nbsp;</p> 2007-06-11T06:09:06-04:001752581http://forums.asp.net/p/1119447/1752581.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>Hi,</p> <p>I guess what you are talking about is the&nbsp;user controls which can add server controls in the ascx page. You can assign the handle method attribute with the method name in the tag. The following one is a sample. For more information about usercontrol, plsase visit <a href="http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx"> http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx</a></p> <p>&nbsp;&nbsp; &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; <strong>OnClick=&quot;Button1_Click&quot;</strong> Text=&quot;Button&quot; /&gt;</p> <p>&nbsp;&nbsp;&nbsp; protected void<strong> Button1_Click</strong>(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; }</p> <p>Thanks.</p> <p>. </p> <p><a href="http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx"></a>&nbsp;</p> 2007-06-13T09:51:00-04:001752655http://forums.asp.net/p/1119447/1752655.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>Hi,</p> <p>&nbsp; You mean to say that We can't do the event handling using the custom server control???</p> <p>&nbsp;</p> <p>&nbsp;</p> 2007-06-13T10:53:21-04:001753246http://forums.asp.net/p/1119447/1753246.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>If you are referring to a Custom Server Control and not a User Control, as I think you are, then perhaps this will help.</p> <p>Say you have a Custom Server Control that is a composition of a ListBox and a Button on a Panel. Your custom server control class would probably look like this</p> <pre class="prettyprint">public class MyCustomListBox : Panel, INamingContainer { protected ListBox MyListBox; protected Button DeleteButton;</pre> <P>&nbsp;...</P> <P>. In order to handle the events of the sub controls (ListBox and Button), you need to register <STRONG>Event Handlers</STRONG> for them. </P> <P>To do this you basically need a method in your custom control of the right signature (object o, EventArgs e), then create a delegate and assign it to the correct event using event registering operator (+=).</P> <P>e.g to capture the <STRONG><EM>Click</EM></STRONG> event of the button, you would create your handler method <EM>in the custom control </EM>(MyCustomListBox) class </P><pre class="prettyprint"><SPAN class=kwd>protected void</SPAN> MyButton_Click(<SPAN class=kwd>object</SPAN> sender, EventArgs e) { <SPAN class=cmt>//do stuff to handle event here</SPAN> }</pre><pre class="prettyprint">then register the handler <EM>on the button </EM>when you are adding the control to your panel in the CreateChildControls method you will be overriding.</pre><pre class="prettyprint">e.g.</pre><pre class="prettyprint">&nbsp;<pre class="prettyprint"><SPAN class=kwd>protected override void</SPAN> CreateChildControls() { MyButton = <SPAN class=kwd>new</SPAN> Button(); //create the button object MyButton.Text = <SPAN class=st>"Delete item"</SPAN>; //modify any properties you want MyButton.Click += <SPAN class=kwd>new</SPAN> EventHandler(<SPAN class=kwd>this</SPAN>.AddRightButton_Click); //Create a <STRONG>delegate</STRONG> for the handler function and add/register it on the Click event <SPAN class=kwd>this</SPAN>.Controls.Add(MyButton); //Add the button to the <EM>this </EM>(ie. to the panel) <SPAN class=cmt>//more child controls stuff //..</SPAN> }</pre>&nbsp;</PRE><pre class="prettyprint">&nbsp;</pre><pre class="prettyprint">When the control is added to a page and displayed the button will be displayed as part of the panel. When the user clicks on the button the <STRONG>Click</STRONG> event will fire and your MyButton_Click method will be called.</pre><pre class="prettyprint">NB: The name of the button handler is not important, you could call it Fred, just so long as it has the correct signature e.g. <EM>protected void Fred(object o, EventArgs e)</EM></pre> 2007-06-13T16:42:02-04:001754279http://forums.asp.net/p/1119447/1754279.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>Hi,</p> <p>&nbsp; I've sorted out the problem. Now, can anyone suggest me that how can i make radio and check box custom controls</p> <p>&nbsp;</p> 2007-06-14T07:53:34-04:001754710http://forums.asp.net/p/1119447/1754710.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>Ajeet,</p> <p>I think it would be polite to first acknowledge some of the people who have already offered you advice on this topic - othwise, I suspect you will not be getting much more help in the future.</p> 2007-06-14T12:49:38-04:001761012http://forums.asp.net/p/1119447/1761012.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>Hi,</p> <p>&nbsp;&nbsp; Sorry For my attitude. Actually before getting your suggestion, I've done the work. But after getting your suggestion, i realized so. I'm late to say thanx but still I want to say thanks.</p> <p>&nbsp;Again Sorry</p> 2007-06-19T07:46:35-04:001772871http://forums.asp.net/p/1119447/1772871.aspx/1?Re+Custom+Control+Event+Handling+ProblemRe: Custom Control Event Handling Problem <p>Hi,</p> <p>&nbsp; There a bit of problem in my button custom control who can act like hyperlink and simple button, When idouble click over it, the cursor goes no where in the .aspx.cs page. means to say the the wvent is not handled by the double click over the control. Although it is working fine by attaching it in the OnInit()method</p> <font color="#0000ff" size="2">protected</font><font size="2"> </font><font color="#0000ff" size="2">override</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> OnInit(</font><font color="#008080" size="2">EventArgs</font><font size="2"> e)</font><font size="2"> <p>{</p> </font><font color="#0000ff" size="2">this</font><font size="2">.AKS_Button1.Click &#43;=</font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#008080" size="2">EventHandler</font><font size="2">(AKS_Button1_Click);</font><font size="2"></font><font color="#0000ff" size="2">base</font><font size="2">.OnInit(e);</font><font size="2"> <p>}</p> <p></font>this code is working fine, but not the double click.</p> <p>thanks in advance</p> 2007-06-26T11:15:53-04:00