View Single Post
  #1 (permalink)  
Old August 19th, 2010, 10:46 PM
Gayathri79 Gayathri79 is offline
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default Click event not firing for Dynamic button control when it has validation control

Hello,
I am creating few dynamic textboxes and a Button control. I have click event handler for the button control too. After validations, i want that event handler to fire. But its not firing. Its working for the Button control when it is created in Design time.

//i know i can do this all in design view , but i want this to be done Dynamically.

private void Page_Load(object sender, System.EventArgs e)
{

for (int i = 0; i < 3; i++)
{
TextBox tb = new TextBox();
tb.ID = "tx" + i.ToString();
tb.Text = "";
Form.Controls.Add(tb);
Form.Controls.Add(new LiteralControl("<br>"));
RequiredFieldValidator cv = new RequiredFieldValidator();
cv.ID = "cv" + i.ToString();
cv.Text = "Field is required";
cv.ControlToValidate = tb.ID;
cv.ForeColor = Color.Red;
Form.Controls.Add(cv);
Form.Controls.Add(new LiteralControl("<br>"));
}
Button btn = new Button();
btn.ID = "btn";
btn.Text = "submit";
btn.CausesValidation = true;
btn.Click += new EventHandler(btn_Click);//not firing
Form.Controls.Add(btn);
Form.Controls.Add(new LiteralControl("<br>"));

}

protected void btn_Click(object sender, EventArgs e)
{
// trylab is label control created in design view
trylab.Text = "Event handler working";
}

-please let me know where i went wrong.
-Thanks for your time