Answered by:
click event won't fire

Question
-
User-892215765 posted
Hi, I'm trying out an example from the MCAD trainingbook but I can't manage to get the click event from a composite control right. Adding the event to the button (line 38) seems to go fine, but the click function (line 41) won't run. Any idea what's going wrong?
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Text; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 9 namespace compositecontrols 10 { 11 public class mathBox : WebControl 12 { 13 [DefaultValue("0")] 14 15 TextBox txtMath = new TextBox(); 16 Button butSum = new Button(); 17 Label lblResult = new Label(); 18 19 protected override void CreateChildControls() 20 { 21 22 base.CreateChildControls(); 23 24 txtMath.TextMode = TextBoxMode.MultiLine; 25 Controls.Add(txtMath); 26 27 Controls.Add(new LiteralControl("<br>")); 28 29 30 butSum.Text = "Sum"; 31 32 Controls.Add(butSum); 33 34 Controls.Add(new LiteralControl(" Result: <b>")); 35 Controls.Add(lblResult); 36 Controls.Add(new LiteralControl("</b>")); 37 38 butSum.Click += new EventHandler(butSum_Click); 39 } 40 41 public void butSum_Click(object sender, EventArgs e) 42 { 43 Sum(); 44 } 45 46 public string Text 47 { 48 get 49 { 50 EnsureChildControls(); 51 return txtMath.Text; 52 } 53 set 54 { 55 EnsureChildControls(); 56 txtMath.Text = value; 57 } 58 } 59 60 char[] strStep = { '\r' }; 61 62 public string[] Values 63 { 64 get 65 { 66 EnsureChildControls(); 67 return txtMath.Text.Split(strStep); 68 } 69 set 70 { 71 EnsureChildControls(); 72 txtMath.Text = String.Join(" ", value); 73 } 74 } 75 76 public string Result 77 { 78 get 79 { 80 EnsureChildControls(); 81 return lblResult.Text; 82 } 83 } 84 85 public void Sum() 86 { 87 EnsureChildControls(); 88 89 if (txtMath.Text.Length != 0) 90 { 91 string[] arrNums; 92 arrNums = txtMath.Text.Split(strStep); 93 double dblSum = 0; 94 95 foreach (string strCount in arrNums) 96 { 97 try 98 { 99 dblSum += Convert.ToDouble(strCount); 100 } 101 catch 102 { 103 } 104 } 105 106 lblResult.Text = dblSum.ToString(); 107 } 108 else 109 { 110 lblResult.Text = "0"; 111 } 112 } 113 114 protected override void Render(HtmlTextWriter writer) 115 { 116 EnsureChildControls(); 117 txtMath.Width = this.Width; 118 double dHeight = this.Height.Value - butSum.Height.Value; 119 txtMath.Height = Unit.Parse(dHeight.ToString()); 120 base.Render(writer); 121 } 122 } 123 }
Sunday, October 14, 2007 4:16 PM
Answers
-
User563148522 posted
You must be implementing the IPostBackEventHandler
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 16, 2007 9:11 AM
All replies
-
User-1136466523 posted
Hi,
From your description, it seems that the event in your composite control can be raised.
In your code, you have set the event after you add the child control into controls set. (Line38 and Line32) Try to add the event before adding controls.
If the problem still exists, please refer the following articles:
http://www.dotnetjunkies.com/quickstart/aspplus/doc/webctrlauthoring.aspx#postbackevts
http://www.dotnetjunkies.com/quickstart/aspplus/doc/webctrlauthoring.aspx#exposingevtsThanks.
Monday, October 15, 2007 10:49 PM -
User563148522 posted
You must be implementing the IPostBackEventHandler
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 16, 2007 9:11 AM -
User-892215765 posted
I added INamingContainer on line 11.
11 public class mathBox : WebControl, INamingContainer
Now it does work, but I don't get any clue about it. Seems to handle unique names or something [:S]
I don't understand what you mean with implementing the IPostback...
Friday, October 19, 2007 2:34 PM -
User-1136466523 posted
Hi,
If a control wants to capture postback events (form submissions from a client), it must implement the System.Web.UI.IPostBackEventHandler interface. This signals to the ASP.NET page framework that a control wants to be notified of a postback event.
For more information, see http://www.dotnetjunkies.com/quickstart/aspplus/doc/webctrlauthoring.aspx#javascript
Thanks.
Monday, October 22, 2007 4:06 AM