473,473 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Event Handling in custom control

90 New Member
I have created a custom control to get the values and sum them, Here I am able to get display the controls but I am not able to add the values in the text box which means that I am not able to catch the event handling function how is it to be done and
my second question is when I press the sum button the values in the textbox gets erased should I need to set the viewstate property if so how should i do that?
below shown is my code
custom control.cs


Expand|Select|Wrap|Line Numbers
  1.       [DefaultEvent("Click")]
  2.       public class WebCustomControl1 : WebControl
  3.       {
  4.       TextBox value1 = new TextBox();
  5.       TextBox value2 = new TextBox();
  6.       Label output = new Label();
  7.       Button sresult = new Button();
  8.       private string showresult;
  9.       protected override void CreateChildControls()
  10.       {
  11.       //base.CreateChildControls();
  12.       value1.TextMode = TextBoxMode.SingleLine;
  13.       Controls.Add(value1);
  14.       Controls.Add(new LiteralControl("<br>"));
  15.       value2.TextMode = TextBoxMode.SingleLine;
  16.       Controls.Add(value2);
  17.       Controls.Add(new LiteralControl("<br>"));
  18.       sresult.Text = "Show Result";
  19.       Controls.Add(sresult);
  20.       Controls.Add(new LiteralControl("<br>"));
  21.       Controls.Add(new LiteralControl("&nbsp&nbspResult:&nbsp<b>"));
  22.       Controls.Add(output);
  23.       Controls.Add(new LiteralControl("</b>"));
  24.       sresult.Click += new EventHandler(btnsumclicked);
  25.       }
  26.       public event EventHandler Click;
  27.       void btnsumclicked(object sender, EventArgs e)
  28.       {
  29.       int a = int.Parse(value1.Text) + int.Parse(value2.Text);
  30.       showresult = a.ToString();
  31.       }
  32.  
  33.       protected virtual void onClick(EventArgs e)
  34.       {
  35.       if (Click != null)
  36.       {
  37.       Click(this, e);
  38.       }
  39.       }
  40.       }
  41.  
In code behind i did not give any thing how should I handle the event in code behind
Jul 19 '08 #1
1 980
furuknap
2 New Member
Hello,

You need to make sure that CreateChildControls is called early enough in the control life cycle. As you are talking about viewstate I assume you are doing this on an ASP.net web page, so try overriding the onload method and add EnsureChildControls() there:

protected override void OnLoad(EventArgs e)
{
EnsureChildControls();
[...]

If you create the controls too late in the page life cycle the event wireup has already happened and no event handler will fire.

.b
Jul 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
3
by: Arulraja | last post by:
Hello, I have created 2 custom server controls, The parent custom control contains multiple child custom controls. The Child control has a button on it. If I Click the button control, it...
6
by: Steve Caliendo | last post by:
Hi, I'm creating 5 ImageButton controls in the panel control, and I have a unique ID specified for each one. When I click on any one of them, the Page_Load executes (Of course), but how do I...
4
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
4
by: Suzanne | last post by:
Hi all, I'm having problems with datagrids and the currentcellchanged event. My problem is this: I have a datagrid on a form, if the user changes the text in a cell on the datagrid then tries to...
4
by: Mike Gaab | last post by:
I am having two problems: 1. Determing the signature of event handlers for any control that raises events. I have a handle on the arguments but the method name is not clear. 2. What is the...
6
by: mj2736 | last post by:
I created a windows form control that inherits from the standard .Net DataGridView control, to which I've added custom functionality. Instead of using the standard control in my applications, I use...
1
by: raghulvarma | last post by:
I have created a custom control to get the values and sum them, Here I am able to get display the controls but I am not able to add the values in the text box which means that I am not able to catch...
2
by: John Whitworth | last post by:
I'm tying myself up in knots again here. I think my query is a little different from the previous Event handling one, so have started a new thread. In VB2008, I want a reusable control, which...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.