Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 19th, 2010, 10:46 PM
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
  #2 (permalink)  
Old August 20th, 2010, 11:44 AM
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Quote:
Originally Posted by Gayathri79 View Post
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.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
  #3 (permalink)  
Old August 20th, 2010, 12:30 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

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."
  #4 (permalink)  
Old August 20th, 2010, 12:31 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Also i checked the top of the page the AutoEventwireup is set to true.
  #5 (permalink)  
Old August 20th, 2010, 01:09 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #6 (permalink)  
Old August 20th, 2010, 06:58 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thanks. ile try that.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataList Command event not firing on first click nikkk ASP.NET 3.5 Basics 0 September 2nd, 2008 04:15 AM
Dynamic control onclick event mii2029 ASP.NET 3.5 Basics 7 August 27th, 2008 11:19 AM
dynamic button control SKhna ASP.NET 2.0 Basics 1 February 1st, 2008 10:36 AM
Duplicate Control Creation on button click event kapi.goel C# 1 December 21st, 2005 02:42 PM
Custom Validation Control Client side event rodmcleay ASP.NET 1.0 and 1.1 Basics 3 December 17th, 2004 08:47 AM



All times are GMT -4. The time now is 04:49 PM.


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