p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   Click event not firing for Dynamic button control when it has validation control (http://p2p.wrox.com/showthread.php?t=80595)

Gayathri79 August 19th, 2010 10:46 PM

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

chroniclemaster1 August 20th, 2010 11:44 AM

Quote:

Originally Posted by Gayathri79 (Post 262148)
btn.Text = "submit";
btn.CausesValidation = true;
btn.Click += new EventHandler(btn_Click);//not firing
Form.Controls.Add(btn);
Form.Controls.Add(new LiteralControl("<br>"));

I have an idea you can check. In C# the default for AutoEventWireup is true. Check the @Page directive at the top of your file to be sure (though I've never been able to program with it set to false).

That means that you simply declare function name and .NET takes care of the relevant wiring for you. Since you then add the event manually here, this might be the only problem. If I'm right, then try commenting out this one line and then see if it works.

Also, it's nice to see someone (besides me) working programmatically, I find it much more powerful. You may already know this, but you may find the <asp:Literal /> control of interest as well. The Label control wraps the Text you specify in a <span> tag, while the literal control outputs it as is. That means you can specify any XHTML out want in addition to the text, which is a level of flexibility that you can use to do some fairly major custom design.

Gayathri79 August 20th, 2010 12:30 PM

Hi,
Thanks for your time.
Can you be more specific about this following line:?

"That means that you simply declare function name and .NET takes care of the relevant wiring for you. Since you then add the event manually here, this might be the only problem. If I'm right, then try commenting out this one line and then see if it works."

Gayathri79 August 20th, 2010 12:31 PM

Also i checked the top of the page the AutoEventwireup is set to true.

Imar August 20th, 2010 01:09 PM

FYI, AutoEventWireup only works for a number of predefined Page events; not for your own controls. For a list of events it applies to, take a look here: http://odetocode.com/blogs/scott/arc...entwireup.aspx

You can't create useful controls in Page_Load as it's too late for the control to participate in the post back architecture and thus the Click event will never be handled.

Instead, you need to do this in Page_Init. Take a look at the following links for more ideas and examples:

http://geekswithblogs.net/shahed/arc...26/123391.aspx
http://www.google.com/#hl=en&source=...rols+Page_Init

Hope this helps,

Imar

Gayathri79 August 20th, 2010 06:58 PM

Thanks. ile try that.


All times are GMT -4. The time now is 03:13 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.