Handling events in custom control

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


Code: ( text )


[DefaultEvent("Click")]

public class WebCustomControl1 : WebControl

{
TextBox value1 = new TextBox();

TextBox value2 = new TextBox();

Label output = new Label();

Button sresult = new Button();
private string showresult;

protected override void CreateChildControls()

{

//base.CreateChildControls();

value1.TextMode = TextBoxMode.SingleLine;

Controls.Add(value1);

Controls.Add(new LiteralControl("
"));

value2.TextMode = TextBoxMode.SingleLine;

Controls.Add(value2);

Controls.Add(new LiteralControl("
"));

sresult.Text = "Show Result";

Controls.Add(sresult);

Controls.Add(new LiteralControl("
"));

Controls.Add(new LiteralControl("  Result: "));

Controls.Add(output);

Controls.Add(new LiteralControl("
"));

sresult.Click += new EventHandler(btnsumclicked);

}

public event EventHandler Click;

void btnsumclicked(object sender, EventArgs e)

{

int a = int.Parse(value1.Text) + int.Parse(value2.Text);

showresult = a.ToString();

}


protected virtual void onClick(EventArgs e)

{

if (Click != null)

{

Click(this, e);

}

}

}



In code behind i did not give any thing how should I handle the event in code behind

Questions by raghulvarma   answers by raghulvarma

Showing Answers 1 - 3 of 3 Answers

rajesh550

  • Oct 22nd, 2008
 

First you may build the control ex Button b = new Button(); b.Text = "ex"; Then you add the button to other control right ? like this.control.add(b); but before add the button (b) to other control, you add the even to the button (b) because its a button, then it will have even onclick, then you add code b.Click +=new EventHandler(b_Click); b_Click is a function or even handler you want to add, if the button (b) clicked.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions