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 - 1 of 1 Answers
Related Answered Questions
Related Open Questions
Handling events in custom control
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
Profile Answers by raghulvarma Questions by raghulvarma
Questions by raghulvarma answers by raghulvarma
Related Answered Questions
Related Open Questions